w3resource

C wcstombs() function

C wcstombs() function - Convert a wide-character string to a character string

Syntax wcstombs() function

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

The wcstombs() function is used to convert the wide-character string pointed to by string into the multibyte array pointed to by str.

Parameters wcstombs() function

Name Description Required /Optional
str This is the pointer to an array of char elements at least n bytes long. Required
pwcs This is wide-character string to be converted. Required
n This is the maximum number of bytes to be written to str. Required

Return value from wcstombs()

  • If a wide-character code is encountered that does not correspond to a valid character, wcstombs() shall return (size_t)-1.
  • Otherwise, the function returns the number of bytes stored in the character array, not including any terminating null byte.

Example: wcstombs() function

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

/*Source:https://www.ibm.com/docs/en*/
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <wchar.h>
#include <memory.h>
#define  STRLENGTH   10
#define  LOCNAME     "qsys.lib/JA_JP.locale"
#define  LOCNAME_EN  "qsys.lib/EN_US.locale"
int main(void)
{
    char string[STRLENGTH];
    int length, sl = 0;
    wchar_t   wc2[] = L"XYZ";
    wchar_t  wc_string[10];
    mbstate_t ps = 0;
    memset(string, '\0', STRLENGTH);
    wc_string[0] = 0x00C1;
    wc_string[1] = 0x4171;
    wc_string[2] = 0x4172;
    wc_string[3] = 0x00C2;
    wc_string[4] = 0x0000;
    /* In this first example we will convert a wide character string */
    /* to a single byte character string.  We first set the locale   */
    /* to a single byte locale.  We choose a locale with             */
    /* CCSID 37.                                                     */
    if (setlocale(LC_ALL, LOCNAME_EN) == NULL)
        printf("setlocale failed.\n");
    length = wcstombs(string, wc2, 10);
    /* In this case wide characters ABC are converted to */
    /* single byte characters ABC, length is 3.   */
    printf("string = %s, length = %d\n\n", string, length);
    /* Now lets try a multibyte example.  We first must set the */
    /* locale to a multibyte locale.  We choose a locale with     */
    /* CCSID 5026  */
    if (setlocale(LC_ALL, LOCNAME) == NULL)
        printf("setlocale failed.\n");
    length = wcstombs(string, wc_string, 10);
    /* The hex look at string would now be:                    */
    /* C10E417141720FC2   length will be 8                     */
    /* You would need a device capable of displaying multibyte */
    /* characters to see this string.                          */
    printf("length = %d\n\n", length);
}

N.B.: This code is executed in windows 10

Output:

setlocale failed.
string = XYZ, length = 3
setlocale failed.
length = -1

C Programming Code Editor:

Previous C Programming: C mbtowc()
Next C Programming: C wctomb()



Follow us on Facebook and Twitter for latest update.