The function wcstombs () in C

Function 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 ..

Function 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 .

Declare the function wcstombs () in C

Below is the declaration for wcstombs () in C:

 size_t wcstombs ( char * str , const wchar_t * pwcs , size_t n ) 

Parameters

str : This is a pointer to an array of char elements of at least n bytes in length.

pwcs : is a wide-character string that needs to be converted.

n : maximum number of bytes to be written to str.

Returns the value

This function returns the number of bytes (not characters) that were converted and written to str, except for the ending null character. If a multibyte char is encountered, this function returns -1.

For example

The following C program illustrates the usage of wcstombs () in C:

 #include #include #define BUFFER_SIZE 50 int main () { size_t ret ; char * MB = ( char *) malloc ( BUFFER_SIZE ); wchar_t * WC = L "https://QTM.com/thu-vien-c/index.jsp" ; /* chuyen doi thanh chuoi wide-character */ ret = wcstombs ( MB , WC , BUFFER_SIZE ); printf ( "Cac ky tu duoc chuyen doi = %un" , ret ); printf ( "Multibyte char = %snn" , MB ); return ( 0 ); } 

Compiling and running the above C program will result:

According to Tutorialspoint

Previous article: Function memcmp () in C

Next lesson: wctomb () function in C

5 ★ | 2 Vote