Skip to main content
char16_t is a fundamental, distinct character type in C++ introduced in C++11, specifically designed to represent 16-bit wide characters and serve as the standard language-level type for UTF-16 encoded code units.

Type Characteristics

  • Underlying Representation: char16_t is guaranteed by the C++ Standard to have the exact same size, signedness, and alignment requirements as std::uint_least16_t (defined in <cstdint>).
  • Signedness: It is strictly an unsigned type.
  • Size: It is guaranteed to be at least 16 bits wide. Because the sizeof operator returns the size in C++ bytes (where one byte is defined as CHAR_BIT bits), sizeof(char16_t) is not strictly guaranteed to be 2. On systems where CHAR_BIT is 8, sizeof(char16_t) is 2; however, on systems where CHAR_BIT is 16 or greater, sizeof(char16_t) evaluates to exactly 1.
  • Type Distinctness: char16_t is a distinct type, not a typedef. While it shares the memory layout of std::uint_least16_t, it is treated as a unique type by the compiler for the purposes of function overloading and template instantiation.
  • Platform Consistency: Unlike wchar_t, whose size is implementation-defined (typically 16 bits on Windows and 32 bits on POSIX systems), char16_t provides a strict cross-platform guarantee regarding its minimum bit width and intended encoding.

Syntax and Literals

To declare a char16_t character or string literal, prefix the literal with a lowercase u.

Standard Library Integration

The C++ Standard Library provides specialized aliases and traits to support char16_t operations, primarily found in the <string> and <string_view> headers.
Standard stream objects (like std::cout) do not have overloaded insertion operators (<<) for char16_t or std::u16string. Outputting these types directly requires explicit conversion to a supported character type (like char) or the use of standard library conversion facets.
Tired of Poor C++ Skills? Fix That With Deep Grasping!Learn More