Latest web development tutorials

C library functions - wcstombs ()

C standard library - <stdlib.h> C standard library - <stdlib.h>

description

C library functionssize_t wcstombs (char * str, const wchar_t * pwcs, size_t n) the wide-character strings pwcsto multibyte stringstrbegins. There will be at mostn bytes are written in str.

statement

Here is () statement wcstombs function.

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

parameter

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

return value

This function returns the number of bytes written to the conversion and str in, not including the terminating null character. If you encounter an invalid multi-byte characters, the return value of -1.

Examples

The following example demonstrates wcstombs () function is used.

#include <stdio.h>
#include <stdlib.h>

#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 compile and run the above program, which will result in the following:

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

C standard library - <stdlib.h> C standard library - <stdlib.h>