Go to main content

man pages section 3: Basic Library Functions

Exit Print View

Updated: Wednesday, July 27, 2022



wcstombs - convert a wide-character string to a character string

wcstombs_s - convert a wide-character string to a character string with additional safety checks


#include <stdlib.h>

size_t wcstombs(char *restrict s, const wchar_t *restrict pwcs, size_t n);
#define __STDC_WANT_LIB_EXT1__ 1
#include <stdlib.h>

errno_t wcstombs_s(size_t *restrict retval, char *restrict dst,
    rsize_t dstmax, const wchar_t *restrict src, rsize_t len);


The wcstombs() function converts the sequence of wide-character codes from the array pointed to by pwcs into a sequence of characters and stores these characters into the array pointed to by s, stopping if a character would exceed the limit of n total bytes or if a null byte is stored. Each wide-character code is converted as if by a call to wctomb(3C).

The behavior of this function is affected by the LC_CTYPE category of the current locale.

No more than n bytes will be modified in the array pointed to by s. If copying takes place between objects that overlap, the behavior is undefined. If s is a null pointer, wcstombs() returns the length required to convert the entire array regardless of the value of n, but no values are stored.

The wcstombs_s() function is part of the C11 bounds checking interfaces specified in the C11 standard, Annex K. It is similar to the wcstombs() function, but with differing parameters and return type in order to provide additional safety checks in the form of explicit runtime-constraints as defined in the C11 standard. See runtime_constraint_handler(3C) and INCITS/ISO/IEC 9899:2011.

Return Values

If a wide-character code is encountered that does not correspond to a valid character (of one or more bytes each), wcstombs() returns (size_t)-1. Otherwise, wcstombs() returns the number of bytes stored in the character array, not including any terminating null byte. The array will not be null-terminated if the value returned is n.

If neither a runtime constraint violation nor encoding error (the result of a failed conversion) occurred, wcstombs_s() returns zero, otherwise, it returns a non-zero value.


The wcstombs() function may fail if:


A wide-character code does not correspond to a valid character.

The wcstombs_s() function may fail if:


Null pointer is passed.


Size argument is not valid value.


A wide-character code does not correspond to a valid character.


See attributes(7) for descriptions of the following attributes:

Interface Stability
See below

The wcstombs() function can be used safely in multithreaded applications.

The wcstombs_s() function cannot be used safely in a multithreaded application due to the runtime constraint handler. For more information, see the runtime_constraint_handler(3C) man page.

See Also

mblen(3C), mbstowcs(3C), mbstowcs_s(3C), mbtowc(3C), setlocale(3C), wctomb(3C), wctomb_s(3C), attributes(7), standards(7), runtime_constraint_handler(3C)