Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

<uchar.h> — Unicode utilities

The <uchar.h> header provides Unicode code-unit types and functions that convert between the corresponding UTF formats and the multibyte encoding of the current LC_CTYPE locale.

Version: C23; repository compiler flag, production runtime not attested

Syntax

#include <uchar.h>

<uchar.h> declares char8_t, char16_t, char32_t, size_t, and mbstate_t. In conversions, the first three types represent code units of UTF-8, UTF-16, and UTF-32, respectively. A code unit does not always represent a whole character.

The pairs mbrtoc8/c8rtomb, mbrtoc16/c16rtomb, and mbrtoc32/c32rtomb convert between the multibyte encoding of the current LC_CTYPE locale and the corresponding UTF format. The encoding selected at compile time for ordinary literals does not determine which multibyte encoding these functions use. Do not assume the current multibyte encoding is UTF-8; preserve mbstate_t state between related calls.

The example counts the code units in a UTF-8 literal, excluding the terminator, independently of locale. It does not call any of these conversion functions. The repository compiler flag does not attest production runtime support.

Examples

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

#if !defined(__STDC_VERSION_UCHAR_H__)
typedef unsigned char char8_t;
#endif

int main(void)
{
    const char8_t text[] = u8"é";
    const size_t units = sizeof text / sizeof text[0] - 1;
    printf("%zu\n", units);
    return 0;
}

Result

2

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources