Skip to content
Tutorials Center
My learning

Topics

C++ Reference · Strings and I/O

Unicode in C++: char8_t and UTF-8 strings

How C++ handles Unicode: char8_t, char16_t, char32_t, u8/u/U literals, std::u8string, UTF-8 byte counts, code points versus characters, and printing text.

C++ has no built-in "Unicode string" type. It has character types and literal prefixes, and a library that treats strings as plain sequences of code units. A code unit is one element of the string (a byte for UTF-8, a 16-bit value for UTF-16). A code point is one Unicode number such as U+00E9 (the letter é). One code point takes between one and four UTF-8 code units, so size() of a UTF-8 string counts bytes, not letters.

The types are char8_t (C++20, UTF-8), char16_t (UTF-16) and char32_t (UTF-32, one code unit per code point). Their literals are u8"...", u"..." and U"...". The most common practical choice is ordinary std::string holding UTF-8, which works with files, networks and the console on most systems.

C++
std::u8string  a = u8"café";    // C++20: 5 char8_t elements
std::u16string b = u"café";     // 4 char16_t elements
std::u32string c = U"café";     // 4 char32_t elements

Try it

C++ Code units versus code points

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ Counting and encoding code points by hand

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
char8_t   // C++20: UTF-8 code unit, unsigned, distinct type from unsigned char
char16_t  // UTF-16 code unit
char32_t  // UTF-32 code unit (one code point)

u8"text"  // const char8_t[N]  (const char[N] before C++20)
u"text"   // const char16_t[N]
U"text"   // const char32_t[N]
'a'  u8'a'  u'a'  U'a'          // character literals (u8 characters: C++17, ASCII only)

std::u8string, std::u16string, std::u32string   // std::basic_string specializations
Escape Meaning
\uXXXX A code point up to U+FFFF (4 hex digits).
\UXXXXXXXX Any code point (8 hex digits).
\xHH One code unit with this value (use with care in u8 strings).
\N{name} Named escape (C++23).

Usage notes

  • char8_t is a separate type since C++20. u8"..." no longer converts to const char*, and std::cout << std::u8string is deleted. Convert with reinterpret_cast<const char*>(...), or keep text in std::string, which holds UTF-8 bytes just as well.
  • size() counts code units. Use a loop like the one above (or a library) to count code points. Even a code point is not always one visible "character": an e followed by a combining accent are two code points that display as one letter (a grapheme cluster).
  • Sorting, case conversion, comparison and normalization are not Unicode-aware in the standard library. std::toupper works on single bytes, std::sort on strings compares code units. Use ICU or another library for linguistically correct behavior.
  • Source file encoding. GCC and Clang read UTF-8 source by default, MSVC needs /utf-8. Plain "text" literals contain whatever bytes are in the file; the execution character set is UTF-8 on most platforms but not guaranteed. Windows consoles may need SetConsoleOutputCP(CP_UTF8).
  • wchar_t and std::wstring are 16 bits on Windows (UTF-16) and 32 bits elsewhere (UTF-32). That is why portable code avoids them.
  • Conversion facilities. std::wstring_convert, std::wbuffer_convert and the std::codecvt facets for UTF conversions are deprecated (C++17, C++20 for the char8_t ones). The standard has no replacement yet, so projects write small converters or use a library. The C functions mbrtoc8, c8rtomb and friends (C23 and C++20 <cuchar>) exist but depend on the locale.
  • Embedded NUL bytes (\0) are legal in std::string and std::u8string and count toward size().