Table of Contents

<uchar.h>

<uchar.h> provides char16_t and char32_t with fixed, portable UTF-16 and UTF-32 encodings (C11). Conversion functions translate between multibyte (UTF-8) and these encodings, suitable for portable Unicode handling.

Use this instead of wchar_t for Unicode code that must work identically across platforms.

Example

This example converts a UTF-8 string to UTF-32 code points and displays them.

// compile: gcc -o ucharexample ucharexample.c
// run: ./ucharexample
// description: convert UTF-8 to UTF-32 and iterate code points
 
#include <uchar.h>
#include <stdio.h>
#include <string.h>
#include <locale.h>
 
int main() {
    setlocale(LC_ALL, "");
 
    const char* utf8 = "héllo";
    mbstate_t state = {0};
    char32_t c32;
    size_t n;
 
    while (*utf8) {
        n = mbrtoc32(&c32, utf8, strlen(utf8), &state);
        if (n == 0 || n == (size_t)-1 || n == (size_t)-2) break;
        printf("U+%04X (%zu bytes)\n", (unsigned)c32, n);
        utf8 += n;
    }
 
    return 0;
}