当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


C语言 wctomb()用法及代码示例


描述

C库函数int wctomb(char *str, wchar_t wchar)函数转换宽字符wchar到其多字节表示并将其存储在指向的字符数组的开头str

声明

以下是 wctomb() 函数的声明。

int wctomb(char *str, wchar_t wchar)

参数

  • str- 这是指向一个足以容纳多字节字符的数组的指针,

  • wchar- 这是 wchar_t 类型的宽字符。

返回值

  • 如果 str 不为 NULL,则 wctomb() 函数返回已写入 str 处的字节数组的字节数。如果 wchar 不能表示为多字节序列,则返回 -1。

  • 如果 str 为 NULL,则 wctomb() 函数在编码具有非平凡移位状态时返回非零值,如果编码为无状态,则返回零。

示例

下面的例子展示了 wctomb() 函数的用法。

#include <stdio.h>
#include <stdlib.h>

int main () {
   int i;
   wchar_t wc = L'a';
   char *pmbnull = NULL;
   char *pmb = (char *)malloc(sizeof( char ));

   printf("Converting wide character:\n");
   i = wctomb( pmb, wc );
   printf("Characters converted:%u\n", i);
   printf("Multibyte character:%.1s\n", pmb);

   printf("Trying to convert when target is NULL:\n");
   i = wctomb( pmbnull, wc );
   printf("Characters converted:%u\n", i);
   /* this will not print any value */
   printf("Multibyte character:%.1s\n", pmbnull);
   
   return(0);
}

让我们编译并运行上面的程序,将产生以下结果 -

Converting wide character:
Characters converted:1
Multibyte character:a
Trying to convert when target is NULL:
Characters converted:0
Multibyte character:

相关用法


注:本文由纯净天空筛选整理自 C library function - wctomb()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。