Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

char32_t — character type for UTF-32

char32_t is a built-in, distinct integer type used for UTF-32 code units. The U prefix creates the corresponding character and string literals. The type provides at least 32 value bits, but does not itself validate Unicode or guarantee a size of four bytes.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

char32_t value = U'\U0001F600';
const char32_t text[] = U"A\U0001F600";

char32_t is a built-in character and integer type; declaring it requires no header. It is a distinct type, not an alias for unsigned int or std::uint_least32_t. The latter is its underlying type, providing at least 32 value bits. Therefore, do not assume that sizeof(char32_t) == 4.

The uppercase U matters: U'\U0001F600' is a literal of type char32_t, while U"A\U0001F600" is a zero-terminated array of const char32_t elements encoded in UTF-32. It has three elements: A, U+1F600, and the terminator. Lowercase u denotes UTF-16 and the type char16_t, not char32_t.

In well-formed UTF-32, one code unit represents one Unicode scalar value. However, char32_t itself is an integer type: it can also store values that are invalid in UTF-32, so external data needs validation. The number of code units need not equal the number of user-perceived characters.

In the verified environment, the example prints the numeric value of U+1F600 (128512), followed by the number of code units excluding the terminator (2). That output does not establish portability. If std::size_t is an extended unsigned integer type with a conversion rank higher than that of int, subtracting 1 still leaves a result of type std::size_t. In that case, overload resolution for operator<< when printing the count can be ambiguous, so the example may fail to compile. The conversion to unsigned long prints a number rather than encoding text; displaying the symbol requires separate encoding appropriate for the output. The supplied source is an evolving standard draft, not a frozen C++23 publication.

Examples

#include <iostream>

int main() {
    constexpr char32_t symbol = U'\U0001F600';
    constexpr char32_t text[] = U"A\U0001F600";
    static_assert(text[1] == symbol);
    static_assert(sizeof(text) / sizeof(text[0]) == 3);

    std::cout << static_cast<unsigned long>(symbol) << '\n';
    std::cout << sizeof(text) / sizeof(text[0]) - 1 << '\n';
}

Result

128512
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