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.

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:

Picture 1 of The wctomb () function in C

According to Tutorialspoint

Last lesson: The function mbtowc () in C

Next article: string.h in C

Update 25 May 2019
Category

System

Mac OS X

Hardware

Game

Tech info

Technology

Science

Life

Application

Electric

Program

Mobile