Share via

c++ literal formats

Stewart Patch 20 Reputation points
2026-03-03T16:43:02.81+00:00

what are the formats for C++ character literals of type wchar_t

Developer technologies | C++
Developer technologies | C++

A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.

0 comments No comments
{count} votes

Answer accepted by question author
  1. Jack Dang (WICLOUD CORPORATION) 15,610 Reputation points Microsoft External Staff Moderator
    2026-03-04T02:38:23.3533333+00:00

    Hi @Stewart Patch ,

    Thanks for reaching out.

    In C++, a character literal of type wchar_t is written using the L prefix in front of the character. That prefix tells the compiler you want a wide character instead of a regular char.

    The basic format is:

    L'character'
    

    For example:

    wchar_t ch1 = L'A';
    wchar_t ch2 = L'中';
    

    Both are valid wide character literals. The L is what makes the literal type wchar_t.

    You can also use escape sequences the same way you would with normal character literals:

    wchar_t newline = L'\n';
    wchar_t hexChar = L'\x4E2D';
    wchar_t unicode = L'\u4E2D';
    

    A couple of important notes:

    • The literal must contain exactly one character (after escape processing).
    • The actual size of wchar_t depends on the platform (commonly 2 bytes on Windows and 4 bytes on many Unix/Linux systems).

    For more details, as RLWA32 has mentioned, you can refer to this document.

    Hope this helps! If my answer was helpful - kindly follow the instructions here so others with the same problem can benefit as well.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. RLWA32 52,266 Reputation points
    2026-03-03T16:51:02.8666667+00:00

    Refer to the documentation at String and character literals

    0 comments No comments

  2. Q&A Assist
    2026-03-03T16:43:13.2+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    For character literals of type wchar_t in C++, use the wide-character literal form:

    wchar_t c1 = L'a';      // simple wide character
    wchar_t c2 = L'\n';     // escape sequence
    wchar_t c3 = L'\x41';   // hex escape (A)
    wchar_t c4 = L'\u03A9'; // universal character name (Ω), if representable
    

    Key points from the formats:

    1. Prefix
      • L prefix before the quotes: L'…' creates a wide-character literal of type wchar_t.
    2. Contents allowed inside L'…'
      • A single ordinary character: L'a', L'Z'.
      • An escape sequence:
        • Standard escapes like L'\n', L'\t', L'\\', L'\''.
        • Octal escapes: backslash + 1–3 octal digits, e.g. L'\101'.
        • Hex escapes: \x + 1 or more hex digits, e.g. L'\x41'.
          • For L-prefixed literals, the highest allowed hex value is 0xFFFF.
      • Universal character names (for Unicode code points representable in wchar_t): L'\uXXXX' or L'\UXXXXXXXX' (subject to implementation limits and representation).
    3. Type and value
      • A wide-character literal with a single character/escape/universal character name has type wchar_t.
      • Its value is the numerical value of its encoding in the execution wide-character set, unless the character has no representation there (then the value is implementation-defined).
    4. Multicharacter wide literals
      • If a wide-character literal contains multiple characters/escapes/universal character names, the value is implementation-defined.
    5. Related wide string form (for completeness)
      • Wide-character string literals use L"…" and have type const wchar_t*, e.g. L"hello".

    References:

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.