It is valid in C and C++ to break a string literal because the preprocessor or the compiler will concatenate adjacent string literals.
const char *zStr = "a" "b"; // valid
What happens when string literals are prefixed with L
(wide characters), u
(UTF-16), U
(UTF-32), u8
(UTF-8), and raw string literals (R"foo(this is a "raw string literal" with double quotes)foo"
)?
For example, is the following allowed:
const wchar_t *zStr = L"a" "b"; // valid?
Concatenate string literals in C/C++ A string literal is a sequence of characters, enclosed in double quotation marks (" ") , which is used to represent a null-terminated string in C/C++. If we try to concatenate two string literals using the + operator, it will fail.
Q. C++: What does C++ append to the end of a string literal constant? a null character string is a char array with a null value (0x00) after the last valid character in the string.
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.
In C the type of a string literal is a char[]. In C++, an ordinary string literal has type 'array of n const char'. For example, The type of the string literal "Hello" is "array of 6 const char". It can, however, be converted to a const char* by array-to-pointer conversion.
In C++0x your example is valid according to [lex.string]/p13:
... If one string literal has no encoding-prefix, it is treated as a string literal of the same encoding-prefix as the other operand. ...
In C++03 this same section said that this code had undefined behavior:
... If a narrow string literal token is adjacent to a wide string literal token, the behavior is undefined. ...
Yes, that particular example is allowed by C++0x. Any combination of prefixless and L-prefixed literals will be treated as though all are L-prefixed.
EDIT: Citation -- N3242 (current C++0x working draft) §2.14.5/13:
In translation phase 6 (2.2), adjacent string literals are concatenated. If both string literals have the same encoding-prefix, the resulting concatenated string literal has that encoding-prefix. If one string literal has no encoding-prefix, it is treated as a string literal of the same encoding-prefix as the other operand.
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