CKTIS / C
wcrtomb — convert a wide character to multibyte form
wcrtomb encodes one wchar_t according to LC_CTYPE, preserving conversion state in mbstate_t.
Version: C23; repository compiler flag, production runtime not attested
Syntax
#include <wchar.h>
size_t wcrtomb(char * restrict s, wchar_t wc, mbstate_t * restrict ps);wcrtomb writes wc using LC_CTYPE, not necessarily UTF-8. Provide MB_CUR_MAX bytes; MB_LEN_MAX accommodates any locale.
Initialize mbstate_t with {0} and retain it between characters. ps == NULL selects internal state.
The result counts written bytes, including shift sequences. Failure returns (size_t)-1, sets errno to EILSEQ, and leaves state unspecified.
L'\0' writes a null byte and restores initial state. Other characters add no terminator. s == NULL ignores wc: it converts L'\0' into an internal buffer rather than measuring length.
Examples
#include <limits.h>
#include <locale.h>
#include <stdio.h>
#include <wchar.h>
int main(void)
{
if (setlocale(LC_CTYPE, "C") == NULL)
return 1;
mbstate_t state = {0};
char buffer[MB_LEN_MAX];
size_t n = wcrtomb(buffer, L'A', &state);
if (n == (size_t)-1)
return 1;
printf("bytes=%zu, matches=%d\n", n,
n == 1 && buffer[0] == 'A');
return 0;
}Result
bytes=1, matches=1
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.