Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is letter L used to indicate wide chars strings?

Tags:

c++

For example:

wchar_t* str = L"hello";

Why "L" and no other letter?

like image 837
rubimonkey Avatar asked Mar 12 '12 01:03

rubimonkey


People also ask

What does l string mean?

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.

How do you declare a wide character in the string literal?

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.

What's the difference between char and Wchar?

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.

What is narrow string?

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 .


2 Answers

MSDN claim it stands for string Literal, ie the entered string should not be translated into anything else

like image 169
Martin Beckett Avatar answered Oct 13 '22 07:10

Martin Beckett


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.

like image 33
Kaz Avatar answered Oct 13 '22 07:10

Kaz