转义序列Escape Sequences

由反斜杠 (\) 后接字母或数字组合构成的字符组合称为“转义序列”。Character combinations consisting of a backslash (\) followed by a letter or by a combination of digits are called "escape sequences." 要在字符常量中表示换行符,单引号或某些其他字符,你必须使用转义序列。To represent a newline character, single quotation mark, or certain other characters in a character constant, you must use escape sequences. 转义序列被视为单个字符,因此,它是有效的字符常量。An escape sequence is regarded as a single character and is therefore valid as a character constant.

转义序列通常用于指定操作,例如终端和打印机上的回车和制表符移动。Escape sequences are typically used to specify actions such as carriage returns and tab movements on terminals and printers. 它们还用于提供非打印字符的文本表现形式和通常具有特殊意义的字符,例如双引号 (")。They are also used to provide literal representations of nonprinting characters and characters that usually have special meanings, such as the double quotation mark ("). 下表列出 ANSI 转义序列以及它们所表示的内容。The following table lists the ANSI escape sequences and what they represent.

请注意,在字符序列被错误地解释为三元组的情况下,前接反斜杠 (\?) 的问号指定文本问号。Note that the question mark preceded by a backslash (\?) specifies a literal question mark in cases where the character sequence would be misinterpreted as a trigraph. 有关详细信息,请参阅三元组See Trigraphs for more information.

转义序列Escape Sequences

转义序列Escape Sequence 表示Represents
\a\a 响铃(警报)Bell (alert)
\b\b BackspaceBackspace
\f\f 换页Formfeed
\n 换行New line
\r\r 回车Carriage return
\t\t 水平制表符Horizontal tab
\v\v 垂直制表符Vertical tab
\'\' 单引号Single quotation mark
\"\" 双引号Double quotation mark
\\ 反斜杠Backslash
\?\? 文本问号Literal question mark
\ ooo\ ooo 八进制表示法的 ASCII 字符ASCII character in octal notation
\x hh\x hh 十六进制表示法的 ASCII 字符ASCII character in hexadecimal notation
\x hhhh\x hhhh 十六进制表示法的 Unicode 字符(如果此转义序列用于宽字符常量或 Unicode 字符串文本)。Unicode character in hexadecimal notation if this escape sequence is used in a wide-character constant or a Unicode string literal.

例如,WCHAR f = L'\x4e00'WCHAR b[] = L"The Chinese character for one is \x4e00"For example, WCHAR f = L'\x4e00' or WCHAR b[] = L"The Chinese character for one is \x4e00".

Microsoft 专用Microsoft Specific

如果反斜杠在表中未显示的字符前面,则编译器将未定义的字符作为字符本身进行处理。If a backslash precedes a character that does not appear in the table, the compiler handles the undefined character as the character itself. 例如,\c 被视为 cFor example, \c is treated as an c.

结束 Microsoft 专用END Microsoft Specific

转义序列允许你发送非图形控制字符到显示设备。Escape sequences allow you to send nongraphic control characters to a display device. 例如,ESC 字符 (\033) 通常用作终端或打印机的控制命令的第一个字符。For example, the ESC character (\033) is often used as the first character of a control command for a terminal or printer. 一些转义序列特定于设备。Some escape sequences are device-specific. 例如,垂直制表符和换页符转义序列(\v 和 \f)不会影响屏幕输出,但它们会执行适当的打印机操作。For instance, the vertical-tab and formfeed escape sequences (\v and \f) do not affect screen output, but they do perform appropriate printer operations.

还可以将反斜杠 (\) 用作继续符。You can also use the backslash (\) as a continuation character. 当换行符(等效于按 RETURN 键)紧接反斜杠时,编译器会忽略反斜杠和换行符并将下一行作为前一行的一部分。When a newline character (equivalent to pressing the RETURN key) immediately follows the backslash, the compiler ignores the backslash and the newline character and treats the next line as part of the previous line. 这主要对长于一行的预处理器定义有用。This is useful primarily for preprocessor definitions longer than a single line. 例如: For example:

#define assert(exp) \  
( (exp) ? (void) 0:_assert( #exp, __FILE__, __LINE__ ) )  

另请参阅See Also

C 字符常量C Character Constants