C uchar.h Header

Beginner
6 min read
Updated: Nov 2026
3 examples
C11

What Is uchar.h?

<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 */
UsePrefer
Store a code pointchar32_t
Store a UTF-16 code unitchar16_t
Decode bytes to code pointsmbrtoc32 / mbrtoc16
Encode code points to bytesc32rtomb / c16rtomb

How to Use uchar.h in C

Include the header, store characters as char16_t or char32_t, and convert with the mbrto* functions.

  1. Add #include <uchar.h> and compile with -std=c11.
  2. Use u'x' / U'x' constants and u"" / U"" strings for Unicode literals.
  3. Zero-initialize an mbstate_t before the first conversion.
  4. Call mbrtoc32 repeatedly, advancing by the byte count it returns.
  5. Check for (size_t)-1 (invalid sequence) and (size_t)-2 (incomplete sequence).
PieceRequired?Role
#include <uchar.h>YesDefines char16_t, char32_t, and the conversion functions
mbstate_tFor conversionsHolds the conversion state; set it to zero first
setlocale()For non-ASCII bytesConversions 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

Cheat Sheet

Main names in uchar.h. C23 adds char8_t, mbrtoc8, and c8rtomb.

NameMeaning
char16_tUTF-16 code unit type (like uint_least16_t)
char32_tUTF-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, mbrtoc32Multibyte to char16_t / char32_t
c16rtomb, c32rtombchar16_t / char32_t to multibyte
__STDC_UTF_16__, __STDC_UTF_32__Defined when the types use UTF-16 / UTF-32
mbstate_tConversion state object

Worked Walkthrough — From Bytes to Code Points

Trace Unicode literals and a step-by-step mbrtoc32 decode of "Hi!".

Calculation
U'\u20AC'   -> char32_t holding U+20AC (euro sign)
u'A'        -> char16_t holding 65 (U+0041)

mbrtoc32 over "Hi!" (ASCII works in any locale):
  call 1: 'H' -> U+0048, consumed 1 byte
  call 2: 'i' -> U+0069, consumed 1 byte
  call 3: '!' -> U+0021, consumed 1 byte
  call 4: '\0' -> returns 0, loop ends

C Examples

Compile with gcc file.c -std=c11 -o out.

Example 1 — Store Unicode Characters

Use char16_t and char32_t constants and print their numeric values.

C
#include <stdio.h>
#include <uchar.h>

int main(void) {
    char16_t a = u'A';
    char32_t euro = U'\u20AC';

    printf("char16_t 'A' = %u\n", (unsigned)a);
    printf("char32_t euro = U+%04X\n", (unsigned)euro);
    printf("sizes: %zu and %zu\n", sizeof(char16_t), sizeof(char32_t));
    return 0;
}

How It Works

  1. Typed constants. u'A' is a char16_t; U'\u20AC' is a char32_t.
  2. Print as numbers. Cast to unsigned for printf.
  3. Sizes. char16_t is at least 16 bits and char32_t at least 32 bits; 2 and 4 bytes on common platforms.

Example 2 — Count Code Points in a UTF-32 String

Walk a char32_t string literal and count characters, not bytes.

C
#include <stdio.h>
#include <uchar.h>

int main(void) {
    const char32_t s[] = U"Hi \u00E9\u4E16";
    size_t n = 0;

    while (s[n] != 0) {
        n++;
    }

    printf("code points: %zu\n", n);
    printf("last: U+%04X\n", (unsigned)s[n - 1]);
    return 0;
}

How It Works

  1. Wide elements. Each char32_t holds one whole code point, so indexing is simple.
  2. Count to the terminator. The string has H, i, a space, é, and 世.
  3. 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;
}

How It Works

  1. Zero the state. memset prepares mbstate_t for the first call.
  2. Decode one character. mbrtoc32 writes a code point and returns how many bytes it consumed.
  3. Stop on errors. Zero means the null character; (size_t)-1 and (size_t)-2 mean invalid or incomplete input.
  4. 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.

Performance & Portability Notes

ApproachCostBest for
char32_t array4 bytes per code point; simple indexingIn-memory processing
char16_t array2 bytes per unit; rare characters need 2 unitsInterop with Windows and Java
UTF-8 (u8)1 to 4 bytes per code point; compactStorage 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.

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.

Next: wchar.h

Work with wide characters and strings using wchar.h.

wchar.h tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful