C library function -wcstombs()

C Standard Library - <stdlib.h>

Description

C Library Functionssize_t wcstombs(char *str, const wchar_t *pwcs, size_t n)convert the wide character stringpwcsConvert to astrThe starting multibyte string. There will be at mostnbytes are writtenstrIn.

Declaration

Below is the declaration of the wcstombs() function.

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

Parameter

  • str-- a pointer to an array of char elements, at least n bytes long.
  • pwcs-- the wide-character string to be converted.
  • n-- the maximum number of bytes to be written to str.

Return Value

The function returns the number of bytes converted and written to str, excluding the terminating null character. If an invalid multibyte character is encountered, a value of -1 is returned.

Example

The following example demonstrates the usage of the wcstombs() function.

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

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

C Standard Library - <stdlib.h>

other extensions