C Library Functions -wctomb()

C Standard Library - <stdlib.h>

Description

C Library Functionsint wctomb(char *str, wchar_t wchar)Convert the wide characterwcharConvert it to its multibyte representation and store it instrThe beginning of the character array pointed to.

Declaration

Below is the declaration of the wctomb() function.

int wctomb(char *str, wchar_t wchar)

Parameter

  • str-- a pointer to an array large enough to store a multibyte character.
  • wchar-- a wide character of type wchar_t.

Return Value

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

Example

The following example demonstrates the usage of the wctomb() function.

#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 us compile and run the above program, which will produce the following result:

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

C Standard Library - <stdlib.h>

other extensions