非常教程

C参考手册

C 语法

Escape sequences

转义序列用于表示字符串文字和字符常量中的某些特殊字符。

以下转义序列可用。如果反斜杠后面跟着没有列出的任何字符,则 ISO C 需要诊断:

Escape sequence

Description

Representation

\'

single quote

byte 0x27 (in ASCII encoding)

\"

double quote

byte 0x22 (in ASCII encoding)

\?

question mark

byte 0x3f (in ASCII encoding)

\

backslash

byte 0x5c (in ASCII encoding)

\a

audible bell

byte 0x07 (in ASCII encoding)

\b

backspace

byte 0x08 (in ASCII encoding)

\f

form feed - new page

byte 0x0c (in ASCII encoding)

\n

line feed - new line

byte 0x0a (in ASCII encoding)

\r

carriage return

byte 0x0d (in ASCII encoding)

\t

horizontal tab

byte 0x09 (in ASCII encoding)

\v

vertical tab

byte 0x0b (in ASCII encoding)

\nnn

arbitrary octal value

byte nnn

\xnn

arbitrary hexadecimal value

byte nn

\unnnn

Unicode character that is not in the basic character set.May result in several characters.

code point U+nnnn

\Unnnnnnnn

Unicode character that is not in the basic character set.May result in several characters.

code point U+nnnnnnnn

注意

在八进制转义序列中,\0它是最有用的,因为它代表以空字符结尾的字符串中的终止空字符。

换行字符\n在文本模式 I / O 中使用时有特殊含义:将其转换为特定于 OS 的换行字节或字节序列。

八进制转义序列的长度限制为三个八进制数字,但如果遇到较早遇到的第一个字符不是有效的八进制数字,则会终止。

十六进制转义序列没有长度限制,并且终止于不是有效的十六进制数字的第一个字符。如果由单个十六进制转义序列所表示的值不适合通过在此字符串文字或字符常量使用的字符类型表示的值的范围(charchar16_tchar32_t,或wchar_t),其结果是不确定的。

在狭窄的字符串中的通用字符名文字或16位的字符串文字可以映射到一个以上的字符,例如\U0001f34c是4个char在UTF-8代码单元(\xF0\x9F\x8D\x8C)和2个char16_t代码单元在UTF-16( \uD83C\uDF4C))。

问号转义序列\?用于防止在字符串文本中解释三字符:一个字符串,例如"??/"编译为"\",但如果第二个问号被转义,如 "?\?/",则它变成"??/"

#include <stdio.h>
 
int main(void)
{
    printf("This\nis\na\ntest\n\nShe said, \"How are you?\"\n");
}

输出:

This
is
a
test
 
She said, "How are you?"

参考

  • C11 standard (ISO/IEC 9899:2011):
    • 5.2.2 Character display semantics (p: 24-25)
    • 6.4.4.4 Character constants (p: 67-70)
  • C99 standard (ISO/IEC 9899:1999):
    • 5.2.2 Character display semantics (p: 19-20)
    • 6.4.4.4 Character constants (p: 59-61)
  • C89/C90 standard (ISO/IEC 9899:1990):
    • 2.2.2 Character display semantics
    • 3.1.3.4 Character constants

C 语法相关

1.#define directive
2.#elif directive
3.#else directive
4.#endif directive
5.#error directive
6.#if directive
7.#ifdef directive
8.#ifndef directive
9.#include directive
10.#line directive
11.#pragma directive
12.alignas
13.Alternative operators and tokens
14.Analyzability
15.Arithmetic operators
16.Arithmetic types
17.Array declaration
18.Array initialization
19.ASCII Chart
20.Assignment operators
21. types
22.Basic concepts
23.Bit fields
24.break statement
25.C language
26.C Operator Precedence
27.cast operator
28.character constant
29.Comments
30.Comparison operators
31.compound literals
32.Conditional inclusion
33.Conformance
34.const type qualifier
35.Constant expressions
36.continue statement
37.Declarations
38.do-while loop
39.Enumerations
40.Expressions
41.External and tentative definitions
42.File scope
43.floating constant
44.for loop
45.Function declarations
46.Function definitions
47.Functions
48.Generic selection
49.goto statement
50.Identifier
51.if statement
52.Implicit conversions
53.Increment/decrement operators
54.Initialization
55.inline function specifier
56.integer constant
57.Lifetime
58.Logical operators
59.Lookup and name spaces
60.Main function
61.Member access operators
62.Memory model
63.Objects and alignment
64.Order of evaluation
65.Other operators
66.Phases of translation
67.Pointer declaration
68.Preprocessor
69.restrict type qualifier
70.return statement
71.Scalar initialization
72.Scope
73.sizeof operator
74.Statements
75.static assert declaration
76.Static storage duration
77.Storage-class specifiers
78.string literals
79.Struct and union initialization
80.Struct declaration
81.switch statement
82.Thread storage duration
83.Type
84.Type
85.Typedef declaration
86.Undefined behavior
87.Union declaration
88.Value categories
89.Variadic arguments
90.volatile type qualifier
91.while loop
92._Alignof operator
93._Noreturn function specifier
C

C 语言是一门通用计算机编程语言,应用广泛。C 语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。