设为首页 加入收藏

TOP

2.2 Literal Constants (4)
2013-10-07 15:25:05 来源: 作者: 【 】 浏览:68
Tags:2.2 Literal Constants

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

  1. 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:

  1. newline \n          horizontal tab \t  
  2. vertical tab \v     backspace \b  
  3. carriage return \r  formfeed \f  
  4. alert (bell) \a     backslash \\  
  5. question mark \     single quote \'  
  6. double quote \"  

We can write any character as a generalized escape sequence of the form

  1. \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:

  1. \7 (bell) \12 (newline) \40 (blank)  
  2. \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

  1. \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.

  1. "Hello World!"                  // simple string literal  
  2. ""                              // empty string literal  
  3. "\nCC\toptions\tfile.[cC]\n"    // string literal using newlines and tabs 

字符串字面常量,简称字符串字面量。在C++(www.cppentry.com) 中,字符串字面量的类型是个以常量字符(const char)为元素的数组,它可以退化为指向常量字符的指针。

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇2.2 Literal Constants (5) 下一篇2.2 Literal Constants (3)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·哈希表 - 菜鸟教程 (2025-12-24 20:18:55)
·MySQL存储引擎InnoDB (2025-12-24 20:18:53)
·索引堆及其优化 - 菜 (2025-12-24 20:18:50)
·Shell 中各种括号的 (2025-12-24 19:50:39)
·Shell 变量 - 菜鸟教 (2025-12-24 19:50:37)