String literals

A string literal contains a sequence of characters or escape sequences enclosed in double quotation mark symbols. 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. 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. The type of a wide string literal is array of const wchar_t. Both types have static storage duration.

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.

Read syntax diagramSkip visual syntax diagramString literal syntax
 
             .---------------------.
             V                     |
>>-+---+--"----+-character-------+-+--"------------------------><
   '-L-'       '-escape_sequence-'
 

Multiple spaces contained within a string literal are retained.

Use the escape sequence \n to represent a new-line character as part of the string. Use the escape sequence \\ to represent a backslash character as part of the string. You can represent a single quotation mark symbol either by itself or with the escape sequence \'. You must use the escape sequence \" to represent a double quotation mark.

Outside of the basic source character set, the universal character names for letters and digits are allowed in C++.

The following are examples of string literals:

char titles[ ] = "Handel's \"Water Music\""; 
char *temp_string = "abc" "def" "ghi";  /* *temp_string = "abcdefghi\0" */ 
wchar_t *wide_string = L"longstring";

This example illustrates escape sequences in string literals:

#include <iostream>
using namespace std;

int main () {
       char *s ="Hi there! \n";       
    		cout << s;
       char *p = "The backslash character \\.";
       cout << p << endl;
       char *q = "The double quotation mark \".\n";
       cout << q ;
 }  

This program produces the following output:

Hi there! 
The backslash character \. 
The double quotation mark ".

To continue a string on the next line, use the line continuation character (\ symbol) followed by optional whitespace and a new-line character (required). For example:

char *mail_addr = "Last Name    First Name    MI   Street Address \
       893    City     Province   Postal code ";

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.       */

Note:
When a string literal appears more than once in the program source, how that string is stored depends on whether strings are read-only or writeable. ILE C/C++ may allocate only one location for a read-only string; all occurrences will refer to that one location. However, that area of storage is potentially write-protected. If strings are writeable, then each occurrence of the string will have a separate, distinct storage location that is always modifiable. By default, the compiler considers strings to be writeable. You can use the #pragma strings directive or the PFROPTC compiler option to change the default storage for string literals.

Related information



[ Top of Page | Previous Page | Next Page | Contents | Index ]