设为首页 加入收藏

TOP

2.1 Primitive Built-in Types (6)
2013-10-07 16:15:22 来源: 作者: 【 】 浏览:81
Tags:2.1 Primitive Built-in Types

Escape Sequences

Some characters, such as backspace or control characters, have no visible image. Such characters are nonprintable. Other characters (single and double quotation marks, question mark, and backslash) have special meaning in the language. Our programs cannot use any of these characters directly. Instead, we use an escape sequence to represent such characters. An escape sequence begins with a backslash. The language defines several escape sequences:

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

We use an escape sequence as if it were a single character:

  1. std::cout << ’\n’; // prints a newline   
  2. std::cout << "\tHi!\n"// prints a tab followd by "Hi!" and a newline  

We can also write a generalized escape sequence, which is \x followed by one or more hexadecimal digits or a \ followed by one, two, or three octal digits. The value represents the numerical value of the character. Some examples (assuming the Latin-1 character set):

  1. \7 (bell) \12 (newline) \40(blank)   
  2. \0 (null) \115 (’M’) \x4d (’M’)  

As with an escape sequence defined by the language, we use these escape sequences as we would any other character:

  1. std::cout << "Hi \x4dO\115!\n"// prints Hi MOM! followed by a newline   
  2. std::cout << ’\115’ << ’\n’; // prints M followed by a newline  

Note that if a \ is followed by more than three octal digits, only the first three are associated with the \. For example, "\1234" represents two characters: the character represented by the octal value 123 and the character 4. In contrast, \x uses up all the hex digits following it; "\x1234" represents a single, 16-bit character composed from the bits corresponding to these four hexadecimal digits. Because most machines have 8-bit chars, such values are unlikely to be useful. Ordinarily, hexadecimal characters with more than 8 bits are used with extended characters sets using one of the prefixes from Table 2.2.

Specifying the Type of a Literal

We can override the default type of an integer, floating- point, or character literal by supplying a suffix or prefix as listed in Table 2.2.

  1. L’a’ // wide character literal, type is wchar_t   
  2. u8"hi!" // utf-8 string literal (utf-8 encodes a Unicode character in 8 bits)   
  3. 42ULL // unsigned integer literal, type is unsigned long long   
  4. 1E-3F // single-precision floating-point literal, type is float   
  5. 3.14159L // extended-precision floating-point literal, type is long double  

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇2.1 Primitive Built-in Types (4) 下一篇2.1 Primitive Built-in Types (7)

评论

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

·数据库:推荐几款 Re (2025-12-25 12:17:11)
·如何最简单、通俗地 (2025-12-25 12:17:09)
·什么是Redis?为什么 (2025-12-25 12:17:06)
·对于一个想入坑Linux (2025-12-25 11:49:07)
·Linux 怎么读? (2025-12-25 11:49:04)