Skip to main content
wchar_t is a distinct fundamental character type in C++ designed to represent characters from the largest extended character set specified among the supported locales. Unlike char, which is guaranteed to be at least 8 bits, the size, signedness, and encoding of wchar_t are implementation-defined, allowing it to store wide characters that exceed the memory capacity of a standard byte.

Syntax and Literals

Wide character and wide string literals are denoted by prefixing the literal with the capital letter L.

Memory Footprint and Implementation Variance

Because the C++ standard dictates that wchar_t must be large enough to hold any character of the execution wide-character set, its size varies significantly across platforms. This is a critical architectural consideration:
  • Windows (MSVC): wchar_t is 16 bits (2 bytes). It typically utilizes UTF-16LE encoding.
  • Linux / macOS (GCC/Clang): wchar_t is 32 bits (4 bytes). It typically utilizes UTF-32 encoding.

Underlying Type and Signedness

wchar_t is a built-in keyword in C++, not a typedef (unlike in C, where it is defined in <stddef.h>). However, it shares the same size, signedness, and alignment requirements as another integral type, which is referred to as its underlying type. Depending on the compiler and platform, wchar_t may be signed or unsigned. You can inspect its limits using the <limits> header. Note that in C++20 and later, inserting a wide character into a narrow stream (std::cout) is explicitly deleted to prevent unintended implicit conversions. To print its numeric limits, the value must be explicitly cast to a standard integer type:

Standard Library Integration

The C++ Standard Library provides specialized templates and objects to handle wchar_t natively, mirroring the standard char implementations:
  • Strings: std::wstring is the wchar_t specialization of std::basic_string<T>.
  • I/O Streams: std::wcout, std::wcin, std::wcerr, and std::wclog are provided for wide-character console operations.
  • File Streams: std::wifstream and std::wofstream handle wide-character file I/O.
  • C-Style Functions: The <cwchar> and <cwctype> headers provide wide-character equivalents to standard C string functions (e.g., wcslen instead of strlen, iswalpha instead of isalpha).
Tired of Poor C++ Skills? Fix That With Deep Grasping!Learn More