Go to main content

man pages section 3: Basic Library Functions

Exit Print View

Updated: Wednesday, July 27, 2022
 
 

wctomb(3C)

Name

wctomb - convert a wide-character code to a character

wctomb_s - convert a wide-character code to a character with additional safety checks

Synopsis

#include <stdlib.h>

int wctomb(char *s, wchar_t wchar);
#define __STDC_WANT_LIB_EXT1__ 1
#include <stdlib.h>

errno_t wctomb_s(int *restrict status, char *restrict s,
    rsize_t smax, wchar_t wc);

Description

The wctomb() function determines the number of bytes needed to represent the character corresponding to the wide-character code whose value is wchar. It stores the character representation (possibly multiple bytes) in the array object pointed to by s (if s is not a null pointer). At most MB_CUR_MAX bytes are stored.

A call with s as a null pointer causes this function to return 0. The behavior of this function is affected by the LC_CTYPE category of the current locale.

The wctomb_s() function is part of the C11 bounds checking interfaces specified in the C11 standard, Annex K. It is similar to the wctomb() function, but with additional parameter testing and explicit runtime-constraints as defined by the C11 standard. See runtime_constraint_handler(3C) and INCITS/ISO/IEC 9899:2011.

Return Values

If s is a null pointer, the wctomb() function returns zero. If s is not a null pointer, the wctomb() function returns -1 if the value of wchar does not correspond to a valid character, or returns the number of bytes that constitute the character corresponding to the value of wchar.

In no case will the value returned be greater than the value of the MB_CUR_MAX macro.

Upon successful completion, the wctomb_s() function returns zero. If a runtime-constraint violation occurred, or if wc was not a valid multibyte character, the wctomb_s() function returns a non-zero value.

The value of int pointed to by status will never be set to a value greater than the value of the MB_CUR_MAX macro.

Errors

No errors are defined for wctomb().

The wctomb_s() function will fail if:

ERANGE

Size argument is not valid value

EOVERFLOW

Destination array is too small

EILSEQ

Illegal byte sequence

Attributes

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

ATTRIBUTE TYPE
ATTRIBUTE VALUE
CSI
Enabled
Interface Stability
Standard
MT-Level
See below

The wctomb() function can be used safely in multithreaded applications, as long as setlocale(3C) is not being called to change the locale.

The wctomb_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), wcstombs(3C), wcstombs_s(3C), attributes(7), standards(7), runtime_constraint_handler(3C)