What is wcstombs in C.

C library functions - wcstombs ()

C Standard Library -


C - library functionssize_t wcstombs (char * str, const wchar_t * pwcs, size_t n) the wide strings pwcsto multibyte stringstbegins. It will at mostn bytes in stwrittenbecome.


Here is () statement wcstombs function.

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


  • str point to a char array element at least n bytes long.
  • pwcs - are converted to wide strings.
  • n - are written to the maximum number of bytes in str.

Return value

This function returns the number of bytes written to the cast and str without the terminating null character. If you encounter an invalid multi-byte character, the return value is -1.


The following example shows the wcstombs () function being used.

#include #include #define BUFFER_SIZE 50 int main () {size_t ret; char * MB = (char *) malloc (BUFFER_SIZE); wchar_t * WC = L "http://www.w3cschool.cc"; / * 转换 宽 字符 字符串 * / ret = wcstombs (MB, WC, BUFFER_SIZE); printf ("要 转换 的 字符 数 =% u \ n", ret); printf ("多 字节 字符 =% s \ n \ n", MB); return (0); }

Let's put together and run the above program that will result in the following:

要 转换 的 字符 数 = 23 多 字节 字符 = http://www.w3cschool.cc

C Standard Library -