1.5 String Literals

A “string literal” is a sequence of characters from the source character set enclosed in double quotation marks (" "). String literals are used to represent a sequence of characters, which, taken together, form a null-terminated string. You must always prefix wide-string literals with the letter L.

Syntax

string-literal :
"s-char-sequence opt"
L"s-char-sequence opt"

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

s-char
:
any member of the source character set except the double
quotation mark ("), backslash (\), or newline character
escape-sequence

The example below is a simple string literal:

char amessage = “This is a string literal.”

All escape codes listed in Table 1.4 are valid in string literals. To represent a double quotation mark in a string literal, use the escape sequence \". The single quotation mark (') can be represented without an escape sequence. The backslash (\) must be followed with a second backslash (\\) when it appears within a string. When a backslash appears at the end of a line, it is always interpreted as a line-continuation character.

Type for String Literals

String literals have type array of char (that is, char[ ]). (Wide-character strings have type array of wchar_t (that is, wchar_t[ ]).) This means that a string is an array with elements of type char. The number of elements in the array is equal to the number of characters in the string plus one for the terminating null character.

Storage of String Literals

The characters of a literal string are stored in order at contiguous memory locations. An escape sequence (such as \\ or \") within a string literal counts as a single character. A null character (represented by the \0 escape sequence) is automatically appended to, and marks the end of, each string literal. (This occurs during translation phase 7, which is described on topic .) Note also that the compiler may not store two identical strings at two different addresses.

String Literal Concatenation

To form string literals that take up more than one line, you can concatenate the two strings. To do this, type a backslash, then press the RETURN key. The backslash causes the compiler to ignore the following newline character. For example, the string literal

“Long strings can be bro\

ken into two or more pieces.”

is identical to the string

“Long strings can be broken into two or more pieces.”

String concatenation can be used anywhere you might previously have used a backslash followed by a newline character to enter strings longer than one line. Because strings can start in any column of the source code without affecting their on-screen representation, you can position strings to enhance source-code readability.

To force a new line within a string literal, enter the newline escape sequence (\n) at the point in the string where you want the line broken, as follows:

“Enter a number between 1 and 100\nOr press Return”

Long strings can be continued in any column of a succeeding line without affecting their appearance when output. For example:

printf ( “This is the first half of the string,”

“ this is the second half” ) ;

As long as each part of the string is enclosed in double quotation marks, the parts are concatenated and output as a single string. This concatenation occurs according to the sequence of events during compilation specified by translation phases. See topic for information on translation phases.

This is the first half of the string, this is the second half

A string pointer, initialized as two distinct string literals separated only by white space, is stored as a single string (pointers are discussed in “Pointer Declarations”). When properly referenced, as in the following example, the result is identical to the previous example:

char *string = “This is the first half of the string,”

“ this is the second half”;

printf( “%s” , string ) ;

In translation phase 6, the multibyte-character sequences specified by any sequence of adjacent string literals or adjacent wide-string literals are concatenated into a single multibyte-character sequence. Therefore, do not design programs to allow modification of string literals during execution. The ANSI standard specifies that the result of modifying a string is undefined.

Storage Class for Strings

Microsoft Specific

Strings have static storage duration. See “Storage Classes”for information about storage duration.¨Maximum String Length

Microsoft Specific

ANSI compatibility requires a compiler to accept up to 509 characters in a string literal after concatenation. The maximum length of a string literal allowed with Microsoft C is 4,096 bytes. ¨