String Literals

A string literal contains a sequence of characters or escape sequences enclosed in double quotation mark symbols.

             .---------------------.
             V                     |
>>-+---+--"----+-character-------+-+--"------------------------><
   '-L-'       '-escape_sequence-'
 
 

The universal character name for a character outside the basic source character set is allowed.

A string literal with the prefix L is a wide string literal. A string literal without the prefix L is an ordinary or narrow string literal.

C The type of a narrow string literal is array of char and the type of a wide string literal is array of wchar_t.

C++The type of a narrow string literal is array of const char and the type of a wide string literal is array of const wchar_t. Both types have static storage duration.

The following are examples of string literals:

char titles[ ] = "Handel's \"Water Music\"";
char *mail_addr = "Last Name    First Name    MI   Street Address \
   City     Province   Postal code ";
char *temp_string = "abc" "def" "ghi";  /* *temp_string = "abcdefghi\0" */
wchar_t *wide_string = L"longstring";

A null ('\0') character is appended to each string. For a wide string literal, the value '\0' of type wchar_t is appended. By convention, programs recognize the end of a string by finding the null character.

Multiple spaces contained within a string literal are retained.

To continue a string on the next line, use the line continuation character (\ symbol) followed by optional whitespace and a new-line character (required). In the following example, the string literal second causes a compile-time error.

char *first = "This string continues onto the next\
  line, where it ends.";                /* compiles successfully.   */
char *second = "The comment makes the \ /* continuation symbol      */
  invisible to the compiler.";          /* compilation error.       */

Concatenation

Another way to continue a string is to have two or more consecutive strings. Adjacent string literals will be concatenated to produce a single string. If a wide string literal and a narrow string literal are adjacent to each other, the resulting behavior is undefined. The following example demonstrates this:

"hello " "there"     /* is equivalent to "hello there"                      */
"hello " L"there"    /* the behavior at the C89 language level is undefined */
"hello" "there"      /* is equivalent to "hellothere"                       */

Characters in concatenated strings remain distinct. For example, the strings "\xab" and "3" are concatenated to form "\xab3". However, the characters \xab and 3 remain distinct and are not merged to form the hexadecimal character \xab3.

C If a wide string literal and a narrow string literal are adjacent, the result is a wide string literal.

Following any concatenation, '\0' of type char is appended at the end of each string. C++ programs find the end of a string by scanning for this value. For a wide string literal, '\0' of type wchar_t is appended. For example:

char *first = "Hello ";            /* stored as "Hello \0"       */
char *second = "there";            /* stored as "there\0"        */
char *third = "Hello " "there";    /* stored as "Hello there\0"  */

Related References

IBM Copyright 2003