The wctomb () function in C
The function int wctomb (char * str, wchar) converts the wide char into its multi-byte representation and stores it at the beginning of the character array pointed to by str .
Declare the function wctomb () in C
Below is the declaration for wctomb () in C:
int wctomb ( char * str , wchar )
Parameters
str : is a pointer to an array large enough to hold char multibytes
wchar: is a wide character of type wchar_t.
Returns the value
If str is not NULL, wctomb () returns the number of bytes that were written to the byte array at str. If you can't represent wchar in multibyte sequence form, this function returns -1.
If str is NULL, the wctomb () function returns a non-zero value if the encoding is status, or value 0 if the encoding is non-state.
For example
The following C program illustrates the usage of wctomb () in C:
#include #include int main () { int i ; wchar_t wc = L 'a' ; char * pmbnull = NULL ; char * pmb = ( char *) malloc ( sizeof ( char )); printf ( "Chuyen doi wide char:n" ); i = wctomb ( pmb , wc ); printf ( "Cac ky tu duoc chuyen doi: %un" , i ); printf ( "Multibyte char: %.1sn" , pmb ); printf ( "Co gang thuc hien thao tac chuyen doi khi dich la NULL:n" ); i = wctomb ( pmbnull , wc ); printf ( "Cac ky tu duoc chuyen doi: %un" , i ); /* lenh sau se khong in bat ky gia tri nao */ printf ( "Multibyte char: %.1sn" , pmbnull ); return ( 0 ); }
Compiling and running the above C program will result:
According to Tutorialspoint
Last lesson: The function mbtowc () in C
Next article: string.h in C
You should read it
May be interested
- string.h in Cthe file header named string.h in c library defines a variable type, a macro and various functions to manipulate character arrays.
- The memchr () function in Cthe function void * memchr (const void * str, int c, size_t n) looks for the first occurrence of the character c (an unsigned char) in the first n bytes of the string pointed to by parameter str.
- The function memcmp () in Cthe function int memcmp (const void * str1, const void * str2, size_t n)) compares the first n bytes of two strings str1 and str2.
- The function wcstombs () in Cfunction size_t wcstombs (char * str, const wchar_t * pwcs, size_t n) convert wide-char string defined by pwcs into a multibyte string starting at str. almost all n bytes are written to str ..
- The function memcpy () in Cfunction void * memcpy (void * str1, const void * str2, size_t n) copies n characters from str2 to str1.
- The strstr () function in Cchar * function strstr (const char * haystack, const char * needle) looks for the final occurrence of the whole needle string (not including the ending null character) that is present in the haystack string.