String literals
suggest changeA string literal is a sequence of characters enclosed in double quotation marks. It denotes an array, not a pointer, and the array includes a terminating null character.
"abcd" /* Array containing 'a', 'b', 'c', 'd', and '\0'. */
An ordinary string literal has element type char. Its characters must not be modified, even though the array type is not const-qualified:
const char *message = "hello";
/* message[0] = 'H'; would have undefined behavior. */
Prefixes select other character types and encodings:
| Prefix | Element type | Encoding |
|---|---|---|
| none | char |
execution character set |
L |
wchar_t |
implementation-defined wide encoding |
u8 |
char through C17; char8_t in C23 |
UTF-8 |
u |
char16_t |
UTF-16 |
U |
char32_t |
UTF-32 |
The char8_t, char16_t, and char32_t names are declared by <uchar.h>. Their exact status and the rules for prefixed literals depend on the selected language version.
Adjacent string literals are concatenated during translation:
const char message[] = "hello, " "world";
Do not use strlen to determine the size of a prefixed literal whose elements are not char; use the corresponding facilities for that character type or sizeof when the array is in scope.