CKTIS / C
c16rtomb — C23 library function
`c16rtomb(s, c16, ps)` converts a `char16_t` unit encoded as UTF-16 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 c16rtomb(char * restrict s, char16_t c16, mbstate_t * restrict ps);c16rtomb(s, c16, ps) converts a char16_t unit encoded as UTF-16 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.
In UTF-16 a non-BMP character can be a surrogate pair: the first-unit call may return 0, and only the second unit completes the character and produces bytes. 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 = c16rtomb(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.