Latest web development tutorials

C library functions - wctomb ()

C standard library - <stdlib.h> C standard library - <stdlib.h>

description

C library functionsint wctomb (char * str, wchar_t wchar) wcharthe wide character into its multibyte representation and stores it at the beginning ofstrpoints to a character array.

statement

Here is () statement wctomb function.

int wctomb(char *str, wchar_t wchar)

parameter

  • str - a pointer to an array large enough to store multi-byte characters.
  • wchar - type wchar_t wide character.

return value

  • If str is not NULL, wctomb () function returns the number of bytes written to the byte array. If wchar can not be represented as a multi-byte sequence, it returns -1.
  • If str is NULL, if the encoder has a shift state, wctomb () function returns nonzero if the encoding is stateless, it returns zero.

Examples

The following example demonstrates wctomb () function is used.

#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("要转换的宽字符:\n");
   i = wctomb( pmb, wc );
   printf("被转换的字符:%u\n", i);
   printf("多字节字符:%.1s\n", pmb);

   printf("当要转换的字符为 NULL 时尝试转换:\n");
   i = wctomb( pmbnull, wc );
   printf("被转换的字符:%u\n", i);
   /* 不会输出任何值 */
   printf("多字节字符:%.1s\n", pmbnull);
   
   return(0);
}

Let's compile and run the above program, which will result in the following:

要转换的宽字符:
被转换的字符:1
多字节字符:a
当要转换的字符为 NULL 时尝试转换:
被转换的字符:0
多字节字符:

C standard library - <stdlib.h> C standard library - <stdlib.h>