C Library Function - wcstombs()
C Standard Library - <stdlib.h>
Description
The C library function sizet wcstombs(char *str, const wchart *pwcs, size_t n) converts the wide-character string pwcs to a multibyte string starting at str. At most n bytes are written to str.
Declaration
Here is the declaration for the wcstombs() function.
size_t wcstombs(char *str, const wchar_t *pwcs, size_t n)
Parameters
str -- 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, not including the terminating null character. If an invalid multibyte character is encountered, it returns -1.
Example
The following example demonstrates the use 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";
/* Convert wide-character string */
ret = wcstombs(MB, WC, BUFFER_SIZE);
printf("Number of characters to convert = %u\n", ret);
printf("Multibyte character = %s\n\n", MB);
return(0);
}
Let's compile and run the above program, which will produce the following result:
Number of characters to convert = 23
Multibyte character = http://www.w3cschool.cc