C Library Function - wctomb()
Description
C library function *int wctomb(char str, wchar_t wchar) Converts the wide character wchar into its multibyte representation and stores it at the start of the str character array.
Declaration
Below is the declaration of the wctomb() function.
int wctomb(char *str, wchar_t wchar)
Parameters
- 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 to the byte array. If wchar cannot be represented as a multibyte sequence, -1 is returned.
- If str is NULL, the wctomb() function returns nonzero if the encoding has a shift state, and zero if the encoding is stateless.
Example
The following example demonstrates the use of the wctomb() function.
#include
#include
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);
/* Does not output any value */
printf("多字节字符:%.1s\n", pmbnull);
return(0);
}
Let's compile and run the above program, which will produce the following result:
Wide characters to be converted:
被转换的字符:1
多字节字符:a
Attempt conversion when the character to be converted is NULL:
被转换的字符:0
Multibyte characters:
C Standard Library -