Defined in header <uchar.h>
size_t c16rtomb( char * restrict s, char16_t c16, mbstate_t * restrict ps );
(since C11)

Converts a single code point from its variable-length 16-bit wide character representation (typically, UTF-16) to its narrow multibyte character representation.

If s is not a null pointer and c16 is the last 16-bit code unit in a valid variable-length encoding of a code point, the function determines the number of bytes necessary to store the multibyte character representation of that code point (including any shift sequences, and taking into account the current multibyte conversion state *ps), and stores the multibyte character representation in the character array whose first element is pointed to by s, updating *ps as necessary. At most MB_CUR_MAX bytes can be written by this function.

If s is a null pointer, the call is equivalent to c16rtomb(buf, u'\0', ps) for some internal buffer buf.

If c16 is the null wide character u'\0', a null byte is stored, preceded by any shift sequence necessary to restore the initial shift state and the conversion state parameter *ps is updated to represent the initial shift state.

If c16 is not the final code unit in a 16-bit representation of a wide character, it does not write to the array pointed to by s, only *ps is updated.

If the macro __STDC_UTF_16__ is defined, the 16-bit encoding used by this function is UTF-16; otherwise, it is implementation-defined. In any case, the multibyte character encoding used by this function is specified by the currently active C locale.


s - pointer to narrow character array where the multibyte character will be stored
c16 - the 16-bit wide character to convert
ps - pointer to the conversion state object used when interpreting the multibyte string

Return value

On success, returns the number of bytes (including any shift sequences) written to the character array whose first element is pointed to by s. This value may be ​0​, e.g. when processing the leading char16_t units in a multi-char16_t-unit sequence (occurs when processing the leading surrogate in a surrogate pair of UTF-16).

On failure (if c16 is not a valid 16-bit code unit), returns -1, stores EILSEQ in errno, and leaves *ps in unspecified state.


In C11 as published, unlike mbrtoc16, which converts variable-width multibyte (such as UTF-8) to variable-width 16-bit (such as UTF-16) encoding, this function can only convert single-unit 16-bit encoding, meaning it cannot convert UTF-16 to UTF-8 despite that being the original intent of this function. This was corrected by the post-C11 defect report DR488.


Note: this example assumes the fix for the defect report 488 is applied.

#include <stdio.h>
#include <locale.h>
#include <uchar.h>
#include <stdlib.h>
mbstate_t state;
int main(void)
    setlocale(LC_ALL, "en_US.utf8");
    char16_t in[] = u"zß水🍌"; // or "z\u00df\u6c34\U0001F34C"
    size_t in_sz = sizeof in / sizeof *in;
    printf("Processing %zu UTF-16 code units: [ ", in_sz);
    for(size_t n = 0; n < in_sz; ++n) printf("%#x ", in[n]);
    char out[MB_CUR_MAX * in_sz];
    char *p = out;
    for(size_t n = 0; n < in_sz; ++n) {
        size_t rc = c16rtomb(p, in[n], &state); 
        if(rc == (size_t)-1) break;
        p += rc;
    size_t out_sz = p - out;
    printf("into %zu UTF-8 code units: [ ", out_sz);
    for(size_t x = 0; x < out_sz; ++x) printf("%#x ", +(unsigned char)out[x]);


Processing 6 UTF-16 code units: [ 0x7a 0xdf 0x6c34 0xd83c 0xdf4c 0 ]
into 11 UTF-8 code units: [ 0x7a 0xc3 0x9f 0xe6 0xb0 0xb4 0xf0 0x9f 0x8d 0x8c 0 ]


  • C11 standard (ISO/IEC 9899:2011):
    • The c16rtomb function (p: 399-400)

See also

generates the next 16-bit wide character from a narrow multibyte string
C++ documentation for c16rtomb

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.