For example:
wchar_t* str = L"hello";
Why "L" and no other letter?
L is the prefix for wide character literals and wide-character string literals which tells the compiler that the char or string is of type wide-char.
A wide string literal is a null-terminated array of constant wchar_t that is prefixed by ' L ' and contains any graphic character except the double quotation mark ( " ), backslash ( \ ), or newline character.
char is used for so called ANSI family of functions (typically function name ends with A ), or more commonly known as using ASCII character set. wchar_t is used for new so called Unicode (or Wide) family of functions (typically function name ends with W ), which use UTF-16 character set.
The type of narrow string literal is array of char . The type of a wide character string literal is array of wchar_t Both types have static storage duration. The type of a narrow string literal is array of const char . The type of a wide string literal is array of const wchar_t .
MSDN claim it stands for string Literal, ie the entered string should not be translated into anything else
This is probably because the L
suffix is used on integer constants. It appears to be a case of reuse of an existing suffix as a prefix. The other answer is that this is committee-designed. Don't ask. The third answer is what letter or other mechanism would you have chosen, and can you justify it against the L
convention? Argue both for and against.
Worse than reuse of L
is reuse of keywords like static
and auto
with new meanings. :)
By the way, this is touched upon in the Rationale for ANSI C (from back in 1989). They don't explain why L was chosen. There is just this:
An L prefix distinguishes wide string literals. A prefix (as opposed to suffix) notation was adopted so that a translator can know at the start of the processing of a long string literal whether it is dealing with ordinary or wide characters. (See §2.2.1.2.)
Section 2.2.1.2 provides no additional clues.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With