Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

wchar_t — wide character type

wchar_t is a distinct built-in integral type used for wide characters. The L prefix creates its corresponding literals. Size, signedness, and encoding depend on the implementation, so it is not a portable Unicode character type.

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

Syntax

wchar_t ch = L'A';
const wchar_t text[] = L"AB";

wchar_t is a keyword naming a distinct built-in character type that belongs to the integral types. It is not an alias for char, short, or int; declaring it requires no header. Its underlying type is an implementation-defined signed or unsigned integer type, so do not assume a fixed size or signedness.

The L prefix selects wide literals: L'A' has type wchar_t, while L"AB" is an array of three const wchar_t elements, including the terminating zero. The declaration const wchar_t text[] = L"AB"; creates a separate array containing those elements. The number of array elements is not its size in bytes.

wchar_t guarantees neither UTF-16 nor UTF-32 encoding, and a single value should not be equated with an arbitrary Unicode character or a user-perceived character. Literals prefixed with u and U use char16_t and char32_t, respectively, not wchar_t. When exchanging text between systems, specify the encoding explicitly instead of treating raw wchar_t bytes as a portable format.

The example checks the literal type at compile time, compares values, and counts array elements including the terminating zero. It prints neither wide text nor the size of wchar_t, so its expected output does not depend on that type's width or the terminal's wide-text support.

Examples

#include <iostream>
#include <type_traits>

int main() {
    constexpr wchar_t ch = L'A';
    constexpr wchar_t text[] = L"AB";
    static_assert(std::is_same_v<decltype(L'A'), wchar_t>);
    static_assert(!std::is_same_v<wchar_t, char>);

    std::cout << std::boolalpha << (text[0] == ch) << '\n';
    std::cout << sizeof(text) / sizeof(text[0]) << '\n';
}

Result

true
3

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