Such literals are of type char. We can obtain a wide-character literal of type wchar_t by immediately preceding the character literal with an L, as in
- L' a'
Escape Sequences for Nonprintable Characters
Some characters are nonprintable. A nonprintable character is a character for which there is no visible image, such as backspace or a control character. Other characters have special meaning in the language, such as the single and double quotation marks, and the backslash. Nonprintable characters and special characters are written using an escape sequence. An escape sequence begins with a backslash. The language defines the following escape sequences:
- newline \n horizontal tab \t
- vertical tab \v backspace \b
- carriage return \r formfeed \f
alert (bell) \a backslash \\ question mark \ single quote \' double quote \"
We can write any character as a generalized escape sequence of the form
- \ooo
where ooo represents a sequence of as many as three octal digits. The value of the octal digits represents the numerical value of the character. The following examples are representations of literal constants using the ASCII character set:
- \7 (bell) \12 (newline) \40 (blank)
- \0 (null) \062 (’2’) \115 (’M’)
如果用\ooo 方式书写转义字符,则建议写满三位八进制整数。
The character represented by ’\0’ is often called a “null character,” and has special significance, as we shall soon see.
We can also write a character using a hexadecimal escape sequence
- \xddd
consisting of a backslash, an x, and one or more hexadecimal digits.
如果用\xdd 方式书写转义字符,则建议写满两位十六进制整数。
Character String Literals
All of the literals we’ve seen so far have primitive built-in types. There is one additional literal—string literal—that is more complicated. String literals are arrays of constant characters, a type that we’ll discuss in more detail in Section 4.3 (p. 130).
String literal constants are written as zero or more characters enclosed in double quotation marks. Nonprintable characters are represented by their underlying escape sequence.
- "Hello World!"
- ""
- "\nCC\toptions\tfile.[cC]\n"
字符串字面常量,简称字符串字面量。在C++(www.cppentry.com) 中,字符串字面量的类型是个以常量字符(const char)为元素的数组,它可以退化为指向常量字符的指针。