< c‎ | string‎ | multibyte
Revision as of 13:47, 10 April 2013 by P12 (Talk | contribs)

Template:ddcl list begin <tr class="t-dsc-header">

Defined in header <stdlib.h>

<td></td> <td></td> </tr> <tr class="t-dcl ">

<td >
size_t wcstombs( char          *dst, const wchar_t          *src, size_t len)

<td class="t-dcl-nopad"> </td> <td > (until C99) </td> </tr> <tr class="t-dcl ">

<td >
size_t wcstombs( char *restrict dst, const wchar_t *restrict src, size_t len)

<td class="t-dcl-nopad"> </td> <td > (since C99) </td> </tr> Template:ddcl list end

Converts a sequence of wide characters from the array whose first element is pointed to by src to its narrow multibyte representation that begins in the initial shift state. Converted characters are stored in the successive elements of the char array pointed to by dst. No more than len bytes are written to the destination array.

Each character is converted as if by a call to wctomb, except that the wctomb's conversion state is unaffected. The conversion stops if:

  • The null character was converted and stored.
  • A wchar_t was found that does not correspond to a valid character in the current C locale.
  • The next multibyte character to be stored would exceed len.



In most implementations, this function updates a global static object of type mbstate_t as it processes through the string, and cannot be called simultaneously by two threads, wcsrtombs should be used in such cases.

POSIX specifies a common extension: if dst is a null pointer, this function returns the number of bytes that would be written to dst, if converted. Similar behavior is standard for wcsrtombs.


dst - pointer to narrow character array where the multibyte character will be stored
src - pointer to the first element of a null-terminated wide string to convert
len - number of byte available in the array pointed to by dst

Return value

On success, returns the number of bytes (including any shift sequences, but excluding the terminating '\0') written to the character array whose first element is pointed to by dst.

On conversion error (if invalid wide character was encountered), returns (size_t)-1.


See also

Template:c/string/multibyte/dcl list wcsrtombsTemplate:c/string/multibyte/dcl list mbstowcs
C++ documentation for wcstombs