Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

c32rtomb — C23 library function

`c32rtomb(s, c32, ps)` converts a `char32_t` unit encoded as UTF-32 to the current `LC_CTYPE` multibyte representation, stores at most `MB_CUR_MAX` bytes, and returns the number stored including shift sequences.

Version: C23

Syntax

size_t c32rtomb(char * restrict s, char32_t c32, mbstate_t * restrict ps);

c32rtomb(s, c32, ps) converts a char32_t unit encoded as UTF-32 to the current LC_CTYPE multibyte representation, stores at most MB_CUR_MAX bytes, and returns the number stored including shift sequences.

Use an explicit mbstate_t while converting a stream. The example passes U'A', receives the single-byte character A, and uses mbsinit to confirm that the state remains initial.

One char32_t unit supplies a complete UTF-32 character; unlike the c8/c16 forms, this function does not assemble a character from several input units. A null s performs the equivalent null-character conversion into an internal buffer and restores the initial state. An encoding error returns (size_t)-1, sets errno = EILSEQ, and leaves the state unspecified; a null ps selects internal state for which the implementation need not avoid data races.

Examples

#include <limits.h>
#include <stdio.h>
#include <uchar.h>
#include <wchar.h>

int main(void) {
    mbstate_t state = {0};
    char output[MB_LEN_MAX];
    size_t count = c32rtomb(output, U'A', &state);
    if (count == (size_t)-1) return 1;
    printf("%zu %c %d\n", count, output[0], mbsinit(&state));
    return 0;
}

Result

1 A 1

Use an explicit `mbstate_t` while converting a stream. The example passes `U'A'`, receives the single-byte character `A`, and uses `mbsinit` to confirm that the state remains initial.

Sources