53

I understand what it does: specifies a string literal as a const wchar_t * (wide character string) instead of const char * (plain old characters), but how is it actually defined?

Is it a macro of some sort? Is it an operator for GCC compilers? What is it?

unwind
  • 364,555
  • 61
  • 449
  • 578
user965369
  • 4,613
  • 10
  • 29
  • 43

3 Answers3

77

The literal prefixes are a part of the core language, much like the suffixes:

'a'    // type: char
L'a'   // type: wchar_t

"a"    // type: char[2]
L"a"   // type: wchar_t[2]
U"a"   // type: char32_t[2]

1      // type: int
1U     // type: unsigned int

0.5    // type: double
0.5f   // type: float
0.5L   // type: long double

Note that wchar_t has nothing to do with Unicode. Here is an extended rant of mine on the topic.

Community
  • 1
  • 1
Kerrek SB
  • 428,875
  • 83
  • 813
  • 1,025
  • 2
    Cheers, relating it to the float suffix got my head around it! – user965369 Oct 26 '12 at 12:55
  • 1
    Here is [another good explanation on wchar_t vs Unicode](http://stackoverflow.com/questions/11107608/whats-wrong-with-c-wchar-t-and-wstrings-what-are-some-alternatives-to-wide). – Yakov Galka Oct 26 '12 at 13:16
19

It's called an encoding prefix:

2.14.5 String literals [lex.string]

string-literal:
| encoding-prefixopt" s-char-sequenceopt"
| encoding-prefixoptR raw-string
encoding-prefix:
| u8
| u
| U
| L

and marks a wide string literal:

11) A string literal that begins with L, such as L"asdf", is a wide string literal. A wide string literal has type “array of n const wchar_t”, where n is the size of the string as defined below; it has static storage duration and is initialized with the given characters.

Luchian Grigore
  • 236,802
  • 53
  • 428
  • 594
6

The meaning of L here is wide character: wchar_t. String with L is coded in 16bit rather than 8bit, take an example:

"A"    = 41
L"A"   = 00 41
whoan
  • 7,411
  • 4
  • 35
  • 46
David
  • 1,466
  • 16
  • 19