<uchar.h> is the C11 header for Unicode character types and conversions. It defines char16_t (a UTF-16 code unit) and char32_t (a UTF-32 code point) and functions that convert between multibyte strings and those types.
String literals pair with these types: u"text" is a char16_t array, U"text" is a char32_t array, and u8"text" is UTF-8. Use mbrtoc32() to decode one multibyte character at a time into a code point.
Remember
#include <uchar.h>
char16_t a = u'A'; /* UTF-16 code unit */
char32_t b = U'\u20AC'; /* UTF-32 code point (euro sign) */
const char32_t s[] = U"Hi";
mbrtoc32(&c32, bytes, n, &state) /* bytes -> code point */
c32rtomb(buf, c32, &state) /* code point -> bytes */
Use
Prefer
Store a code point
char32_t
Store a UTF-16 code unit
char16_t
Decode bytes to code points
mbrtoc32 / mbrtoc16
Encode code points to bytes
c32rtomb / c16rtomb
Approach
How to Use uchar.h in C
Include the header, store characters as char16_t or char32_t, and convert with the mbrto* functions.
Add #include <uchar.h> and compile with -std=c11.
Use u'x' / U'x' constants and u"" / U"" strings for Unicode literals.
Zero-initialize an mbstate_t before the first conversion.
Call mbrtoc32 repeatedly, advancing by the byte count it returns.
Check for (size_t)-1 (invalid sequence) and (size_t)-2 (incomplete sequence).
Piece
Required?
Role
#include <uchar.h>
Yes
Defines char16_t, char32_t, and the conversion functions
mbstate_t
For conversions
Holds the conversion state; set it to zero first
setlocale()
For non-ASCII bytes
Conversions use the current locale's multibyte encoding
Pseudocode
Pseudocode
state = zeroed mbstate_t
while bytes remain:
n = mbrtoc32(&c32, p, len, &state)
if n == 0 or n is an error: stop
use c32 // one Unicode code point
p += n; len -= n
Reference
Cheat Sheet
Main names in uchar.h. C23 adds char8_t, mbrtoc8, and c8rtomb.
Name
Meaning
char16_t
UTF-16 code unit type (like uint_least16_t)
char32_t
UTF-32 code point type (like uint_least32_t)
u'x', U'x'
Character constants of type char16_t / char32_t
u"…", U"…", u8"…"
String literals in UTF-16, UTF-32, and UTF-8
mbrtoc16, mbrtoc32
Multibyte to char16_t / char32_t
c16rtomb, c32rtomb
char16_t / char32_t to multibyte
__STDC_UTF_16__, __STDC_UTF_32__
Defined when the types use UTF-16 / UTF-32
mbstate_t
Conversion state object
Trace
Worked Walkthrough — From Bytes to Code Points
Trace Unicode literals and a step-by-step mbrtoc32 decode of "Hi!".
Wide elements. Each char32_t holds one whole code point, so indexing is simple.
Count to the terminator. The string has H, i, a space, é, and 世.
Inspect the last one. The final element is U+4E16.
Example 3 — Decode Bytes with mbrtoc32
Convert a narrow string to code points one character at a time.
C
#include <stdio.h>
#include <string.h>
#include <uchar.h>
int main(void) {
const char *p = "Hi!";
size_t len = strlen(p);
mbstate_t state;
char32_t c32;
size_t n;
memset(&state, 0, sizeof state);
while (len > 0) {
n = mbrtoc32(&c32, p, len, &state);
if (n == 0 || n == (size_t)-1 || n == (size_t)-2) {
break;
}
printf("U+%04X\n", (unsigned)c32);
p += n;
len -= n;
}
return 0;
}
Output
U+0048
U+0069
U+0021
How It Works
Zero the state.memset prepares mbstate_t for the first call.
Decode one character.mbrtoc32 writes a code point and returns how many bytes it consumed.
Stop on errors. Zero means the null character; (size_t)-1 and (size_t)-2 mean invalid or incomplete input.
Non-ASCII text. Call setlocale(LC_ALL, "") in a UTF-8 locale first to decode multi-byte characters.
Edge Cases & Pitfalls
Unicode handling in C depends on the locale and on the platform.
locale
Conversion depends on locale
mbrtoc32 decodes using the current locale's multibyte encoding. In the default C locale only ASCII is guaranteed; call setlocale() for UTF-8 text.
pair
char16_t is a code unit
Code points above U+FFFF need two char16_t units (a surrogate pair). mbrtoc16 returns them across two calls.
state
Zero the state
Initialize mbstate_t to zero before the first call and reuse it across one string.
support
Not on every platform
macOS does not ship uchar.h; glibc, musl, and MSVC do.
Analysis
Performance & Portability Notes
Approach
Cost
Best for
char32_t array
4 bytes per code point; simple indexing
In-memory processing
char16_t array
2 bytes per unit; rare characters need 2 units
Interop with Windows and Java
UTF-8 (u8)
1 to 4 bytes per code point; compact
Storage and network text
These types describe encodings but do not add string functions. Use them together with wchar.h and locale.h for full text handling.
Remember
Key Takeaways
Role:char16_t and char32_t for UTF-16 and UTF-32 data.
Literals:u"", U"", and u8"" create Unicode strings.
Convert:mbrtoc32 turns bytes into code points one at a time.
Locale: multibyte conversion follows the current locale.
One line:uchar.h gives C the types and converters it needs to work with Unicode code points.
Frequently Asked Questions
uchar.h is the C11 header that defines char16_t and char32_t and the conversion functions mbrtoc16, c16rtomb, mbrtoc32, and c32rtomb.
char16_t holds a UTF-16 code unit, so some characters need two. char32_t holds a whole Unicode code point in one value.
Prefix the literal with U, for example U"Hi". Use u for UTF-16 and u8 for UTF-8.
It decodes the current locale's multibyte encoding. In a UTF-8 locale (after setlocale) it decodes UTF-8; in the default C locale only ASCII is guaranteed.
The number of bytes consumed, 0 for the null character, (size_t)-1 for an invalid sequence, or (size_t)-2 for an incomplete sequence.
No. macOS does not ship uchar.h. glibc, musl, and MSVC provide it.