String literal
Syntax
" s-char-sequence(optional)"
|
(1) | ||||||||
L" s-char-sequence(optional)"
|
(2) | ||||||||
u8" s-char-sequence(optional)"
|
(3) | (since C++11) | |||||||
u" s-char-sequence(optional)"
|
(4) | (since C++11) | |||||||
U" s-char-sequence(optional)"
|
(5) | (since C++11) | |||||||
prefix(optional) R" d-char-sequence(optional)( r-char-sequence(optional)) d-char-sequence(optional)"
|
(6) | (since C++11) | |||||||
Explanation
s-char-sequence | - | A sequence of one or more s-chars |
s-char | - | One of
|
basic-s-char | - | A character from the source character set (until C++23)translation character set (since C++23), except the double-quote ", backslash \, or new-line character |
prefix | - | One of L , u8 , u , U
|
d-char-sequence | - | A sequence of one or more d-chars, at most 16 characters long |
d-char | - | A character from the basic source character set (until C++23)basic character set (since C++23), except parentheses, backslash and spaces |
r-char-sequence | - | A sequence of one or more r-chars, except that it must not contain the closing sequence) d-char-sequence"
|
r-char | - | A character from the source character set (until C++23)translation character set (since C++23) |
N
is the size of the string in code units of the execution narrow encoding (until C++23)ordinary literal encoding (since C++23), including the null terminator.N
is the size of the string in code units of the execution wide encoding (until C++23)wide literal encoding (since C++23), including the null terminator.N
is the size of the string in UTF-8 code units including the null terminator.N
is the size of the string in UTF-16 code units including the null terminator.N
is the size of the string in UTF-32 code units including the null terminator.Each s-char (originally from non-raw string literals) or r-char (originally from raw string literals) (since C++11) initializes the corresponding element(s) in the string literal object. An s-char or r-char (since C++11) corresponds to more than one element if and only if it is represented by a sequence of more than one code units in the string literal's associated character encoding.
If a character lacks representation in the associated character encoding,
|
(since C++23) |
Each numeric escape sequence corresponds to a single element. If the value specified by the escape sequence fits within the unsigned version of the element type, the element has the specified value (possibly after conversion to the element type); otherwise (the specified value is out of range), the string literal is ill-formed. (since C++23)
Concatenation
String literals placed side-by-side are concatenated at translation phase 6 (after the preprocessor). That is, "Hello," " world!" yields the (single) string "Hello, world!". If the two strings have the same encoding prefix (or neither has one), the resulting string will have the same encoding prefix (or no prefix).
If one of the strings has an encoding prefix and the other doesn't, the one that doesn't will be considered to have the same encoding prefix as the other. L"Δx = %" PRId16 // at phase 4, PRId16 expands to "d" // at phase 6, L"Δx = %" and "d" form L"Δx = %d" If a UTF-8 string literal and a wide string literal are side by side, the program is ill-formed. |
(since C++11) |
Any other combination of encoding prefixes may or may not be supported by the implementation. The result of such a concatenation is implementation-defined. |
(since C++11) (until C++23) |
Any other combination of encoding prefixes is ill-formed. |
(since C++23) |
Notes
The null character ('\0', L'\0', char16_t(), etc) is always appended to the string literal: thus, a string literal "Hello" is a const char[6] holding the characters 'H', 'e', 'l', 'l', 'o', and '\0'.
The encoding of ordinary string literals (1) and wide string literals (2) is implementation-defined. For example, gcc selects them with the command line options -fexec-charset and -fwide-exec-charset.
String literals have static storage duration, and thus exist in memory for the life of the program.
String literals can be used to initialize character arrays. If an array is initialized like char str[] = "foo";, str will contain a copy of the string "foo".
Whether string literals can overlap and whether successive evaluations of a string-literal yield the same object is unspecified. That means that identical string literals may or may not compare equal when compared by pointer.
bool b = "bar" == 3+"foobar" // could be true or false, implementation-defined
Attempting to modify a string literal results in undefined behavior: they may be stored in read-only storage (such as .rodata
) or combined with other string literals:
const char* pc = "Hello"; char* p = const_cast<char*>(pc); p[0] = 'M'; // undefined behavior
String literals are convertible and assignable to non-const char* or wchar_t* in order to be compatible with C, where string literals are of types char[N] and wchar_t[N]. Such implicit conversion is deprecated. |
(until C++11) |
String literals are not convertible or assignable to non-const CharT*. An explicit cast (e.g. |
(since C++11) |
A string literal is not necessarily a null-terminated character sequence: if a string literal has embedded null characters, it represents an array which contains more than one string.
const char* p = "abc\0def"; // std::strlen(p) == 3, but the array has size 8
If a valid hex digit follows a hex escape in a string literal, it would fail to compile as an invalid escape sequence. String concatenation can be used as a workaround:
//const char* p = "\xfff"; // error: hex escape sequence out of range const char* p = "\xff""f"; // OK: the literal is const char[3] holding {'\xff','f','\0'}
Although mixed wide string literal concatenation is allowed in C++11, all known C++ compilers reject such concatenation, and its usage experience is unknown. As a result, allowance of mixed wide string literal concatenation is removed in C++23.
Example
#include <iostream> char array1[] = "Foo" "bar"; // same as char array2[] = { 'F', 'o', 'o', 'b', 'a', 'r', '\0' }; const char* s1 = R"foo( Hello World )foo"; // same as const char* s2 = "\nHello\n World\n"; // same as const char* s3 = "\n" "Hello\n" " World\n"; const wchar_t* s4 = L"ABC" L"DEF"; // ok, same as const wchar_t* s5 = L"ABCDEF"; const char32_t* s6 = U"GHI" "JKL"; // ok, same as const char32_t* s7 = U"GHIJKL"; const char16_t* s9 = "MN" u"OP" "QR"; // ok, same as const char16_t* sA = u"MNOPQR"; // const auto* sB = u"Mixed" U"Types"; // before C++23 may or may not be supported by // the implementation; ill-formed since C++23 const wchar_t* sC = LR"--(STUV)--"; // ok, raw string literal int main() { std::cout << array1 << ' ' << array2 << '\n' << s1 << s2 << s3 << std::endl; std::wcout << s4 << ' ' << s5 << ' ' << sC << std::endl; }
Output:
Foobar Foobar Hello World Hello World Hello World ABCDEF ABCDEF STUV
Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
CWG 1823 | C++98 | whether string literals are distinct is implementation-defined |
distinctness is unspecified, and same string literal can yield different object |
See also
user-defined literals(C++11) | literals with user-defined suffix |