# 2.13.4 - [lex.string] - 【词法.字符串】

### 2 词法约定 【词法】

#### 2.13.4 字符串文字量 【词法.字符串】

string-literal:
"s-char-sequenceopt"
L"s-char-sequenceopt"

s-char-sequence:
s-char
s-char-sequence s-char

s-char:
any member of the source character set except
the double-quote ", backslash /, or new-line character
escape-sequence
universal-character-name

字符串-文字量:
"s-字符-序列opt"
L"s-字符-序列opt"

s-字符-序列:
s-字符
s-字符-序列 s-字符

s-字符:
源字符集中的任何成员，除了
双引号 "，反斜杠 /，或换行字符
转义-序列
统一字符名称

A string literal is a sequence of characters (as defined in 2.13.2) surrounded by double quotes, optionally beginning with the letter L, as in "..." or L"...". A string literal that does not begin with L is an ordinary string literal, also referred to as a narrow string literal. An ordinary string literal has type "array of n const char" and static storage duration (3.7), where n is the size of the string as defined below, and is initialized with the given characters. 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" and has static storage duration, where n is the size of the string as defined below, and is initialized with the given characters.

Whether all string literals are distinct (that is, are stored in nonoverlapping objects) is implementation-defined. The effect of attempting to modify a string literal is undefined.

In translation phase 6 (2.1), adjacent narrow string literals are concatenated and adjacent wide string literals are concatenated. If a narrow string literal token is adjacent to a wide string literal token, the behavior is undefined. Characters in concatenated strings are kept distinct. [Example:

"/xA" "B"

contains the two characters '/xA' and 'B' after concatenation (and not the single hexadecimal character '/xAB'). ]

"/xA" "B"

After any necessary concatenation, in translation phase 7 (2.1), '/0' is appended to every string literal so that programs that scan a string can find its end.

Escape sequences and universal-character-names in string literals have the same meaning as in character literals (2.13.2), except that the single quote ' is representable either by itself or by the escape sequence /', and the double quote " shall be preceded by a /. In a narrow string literal, a universal-character-name may map to more than one char element due to multibyte encoding. The size of a wide string literal is the total number of escape sequences, universal-character-names, and other characters, plus one for the terminating L'/0'. The size of a narrow string literal is the total number of escape sequences and other characters, plus at least one for the multibyte encoding of each universal-character-name, plus one for the terminating '/0'.

PREV [lex.fcon] | NEXT [lex.bool] 上一页 【词法.浮点常量】 | 下一页 【词法.布尔常量】
• 本文已收录于以下专栏：

举报原因： 您举报文章：2.13.4 - [lex.string] - 【词法.字符串】 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)