非常教程

C参考手册

字符串 | Strings

mbsinit

在头文件<wchar.h>中定义

int mbsinit(const mbstate_t * ps);

(自C95以来)

如果ps不是空指针,则mbsinit函数确定指向的mbstate_t对象是否描述初始转换状态。

注意

尽管零初始化的mbstate_t总是表示初始转换状态,但也可能有其他值也表示初始转换状态的mbstate_t。

参数

PS

-

指向要检查的mbstate_t对象的指针

返回值

如果ps不是空指针,并且不表示初始转换状态,则返回0,否则为非零值。

#include <locale.h>
#include <string.h>
#include <stdio.h>
#include <wchar.h>
 
int main(void)
{
    // allow mbrlen() to work with UTF-8 multibyte encoding
    setlocale(LC_ALL, "en_US.utf8");
    // UTF-8 narrow multibyte encoding
    const char* str = u8"水"; // or u8"\u6c34" or "\xe6\xb0\xb4"
    static mbstate_t mb; // zero-initialize
    (void)mbrlen(&str[0], 1, &mb);
    if (!mbsinit(&mb)) {
        printf("After processing the first 1 byte of %s,\n"
               "the conversion state is not initial\n\n", str);
    }
 
    (void)mbrlen(&str[1], strlen(str), &mb);
    if (mbsinit(&mb)) {
        printf("After processing the remaining 2 bytes of %s,\n"
               "the conversion state is initial conversion state\n", str);
    }
}

输出:

After processing the first 1 byte of 水,
the conversion state is not initial
 
After processing the remaining 2 bytes of 水,
the conversion state is initial conversion state

参考

  • C11标准(ISO / IEC 9899:2011):
    • 7.29.6.2.1 mbsinit函数(p:441-442)
  • C99标准(ISO / IEC 9899:1999):
    • 7.24.6.2.1 mbsinit函数(p:387-388)

扩展内容

mbstate_t(C95)

转换多字节字符串所需的状态信息(类)

| 用于mbsinit的C ++文档 |

C

C 语言是一门通用计算机编程语言,应用广泛。C 语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。