# 附录\*转义字符

通过`puts`可以输出字符串，例如：

```
puts("123abc");
```

"123abc" 对应的ASCII码值的八进制分别是 61、62、63、141、142、143，上面的代码也可以写为：

```
puts("\61\62\63\141\142\143");
```

在C语言中，所有的ASCII码都可以用反斜杠`\`加数字（默认是8进制）来表示，称为

转义字符（Escape Character），因为`\`后面的字符都不是它原来的ASCII字符的意思了。

除了八进制，也可以用十六进制来表示。用十六进制表示时数字要以`x`开头。"123abc" 对应的ASCII码值的十六进制分别是 31、32、33、61、62、63，所以也可以写为：

```
puts("\x31\x32\x33\x61\x62\x63");
```

> 注意：只能使用八进制或十六进制，不能使用十进制。

一个完整的例子：

```c
#include <stdio.h>
int main(){
    puts("The string is: \61\62\63\x61\x62\x63");
    return 0;
}
```

运行结果：

```
The string is: 123abc
```

在ASCII码中，从 0\~31（十进制）的字符为控制字符，它们都是看不见的字符，不能在显示器上显示，也没法书写，只能以转义字符的形式来表示。不过，直接使用ASCII码值记忆不方便，针对常用的控制字符，C语言又定义了简写方式，完整的列表如下：

| 转义字符 | 意义                    | ASCII码值（十进制） |
| ---- | --------------------- | ------------ |
| \a   | 响铃(BEL)               | 007          |
| \b   | 退格(BS) ，将当前位置移到前一列    | 008          |
| \f   | 换页(FF)，将当前位置移到下页开头    | 012          |
| \n   | 换行(LF) ，将当前位置移到下一行开头  | 010          |
| \r   | 回车(CR) ，将当前位置移到本行开头   | 013          |
| \t   | 水平制表(HT) （跳到下一个TAB位置） | 009          |
| \v   | 垂直制表(VT)              | 011          |

转义字符示例：

```c
#include <stdio.h>
int main(){
    puts("C\tC++\tJava\nC first appeared!\a");
    return 0;
}
```

运行结果：

```
C       C++     Java
C first appeared!
```

同时会听到喇叭发出“嘟”的声音，这是使用`\a`的效果。

## 如何在字符串中输出"和\\

`"`和`\`在字符串中都有特殊含义：`"`表示字符串的开始和结束，`\`表示转义字符的开始。它们都不能直接出现在字符串中，必须要经过转义，也就是在前面加`\`，如`\"`、`\\`。

例如，输出字符串`abc\61"xyz`的代码：

```c
#include <stdio.h>
int main(){
    puts("abc\\61\"xyz");
    return 0;
}
```

运行结果：

```
abc\61"xyz
```

如果`\`不经过转义，写作`puts("abc\61\"xyz");`，那么会输出`abc1"xyz`，`\61`被当做转义字符处理。

如果`"`不经过转义，写作`puts("abc\\61"xyz");`，就会出现错误，编译器会把`"abc\\61"`作为一个字符串，而`xyz"`不知道是什么，也不应该出现在这里，所以报错。

**总结：**&#x5B57;符串中出现`\`、`"`时必须要转义。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://xiaoxiami.gitbook.io/c/zhuan-yi-zi-fu-he-kong-bai-fu.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
