> For the complete documentation index, see [llms.txt](https://labspc.gitbook.io/cnippets/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://labspc.gitbook.io/cnippets/chap2.-lei-xing-yun-suan-fu-yu-biao-da-shi/2.3-chang-liang-mei-ju-yu-zhuan-yi-zi-fu.md).

# 2.3 常量、枚举与转义字符

### 2.3.1 常量 Constant <a href="#ayger" id="ayger"></a>

我们为什么需要变量，也需要常量？

<figure><img src="https://labspc.com/wp-content/uploads/2024/01/1705657722-word-image-283-1.png" alt=""><figcaption></figcaption></figure>

从生活中去理解，提供了一个很好的思路。

常量的分类：

* 字符常量 'a'
* 字符串常量 "a"
* 枚举常量

### 2.3.2 枚举 Enumeration

"Enumeration"（枚举）这个术语源自英语，它是将一组具有相对固定值的常量组织在一起的一种数据类型。枚举类型在计算机编程中非常常见，它允许开发者定义一组有限的命名常量，使代码更易读、更易维护。因此，"Enumeration"被翻译为中文的"枚举"，因为它将一系列可能的值列举出来，程序员可以从中选择。

可以将枚举（`enum`）看作是一种**常量的新的定义类型**。枚举提供了一种将一组相关的命名常量组织在一起的方式，使得代码更加清晰和易读。

通过枚举，您可以为一组相关的常量赋予有意义的名字，并将它们作为一种单独的数据类型来使用。枚举中的每个常量都被赋予一个整数值，这些值默认从0开始递增，但您也可以手动指定每个常量的值。

```c
enum bool { NO, YES };
```

{% code title="2.3.1 enum-weekday.c" lineNumbers="true" %}

```c
// 定义枚举类型
enum Weekday
{
    Monday,    // 0
    Tuesday,   // 1
    Wednesday, // 2
    Thursday,  // 3
    Friday,    // 4
    Saturday,  // 5
    Sunday     // 6
};

int main()
{
    // 声明枚举变量
    enum Weekday today;

    // 给枚举变量赋值
    today = Friday;
    printf("%d\n", Monday);
    // 使用枚举常量
    if (today == Wednesday)
    {
        printf("It's Wednesday!\n");
    }
    else
        printf("It's Saturday.");
    return 0;
}
// Happy Coding. 2024-01-20 01:48 
```

{% endcode %}

### 2.3.3 转义字符 escape character <a href="#io6es" id="io6es"></a>

一些需要了解的知识点：

<figure><img src="https://labspc.com/wp-content/uploads/2024/01/1705657724-word-image-283-2.png" alt=""><figcaption></figcaption></figure>

In English:

In C programming, an escape character is a special character sequence that starts with a backslash (`\`) and is used to represent characters or perform specific actions that are otherwise difficult or impossible to include directly in a string. Escape characters allow you to insert special characters, control characters, and other non-printable characters into your strings, as well as perform certain control operations within your program.

Common escape characters in C include:

1. : Newline character. Moves the cursor to the beginning of the next line, creating a line break.
2. : Tab character. Inserts a horizontal tab position for aligning text.
3. \\: Backslash character. Used to insert a literal backslash.
4. ': Single quote character. Used to insert a single quotation mark.
5. ": Double quote character. Used to insert double quotation marks, often used in string literals.
6. \0: Null character. Signifies the end of a C-style string.
7. \a: Audible bell. Typically triggers a system or console bell sound.
8. \b: Backspace. Moves the cursor to the previous position, often used for simulating a backspace.
9. : Carriage return. Moves the cursor to the beginning of the current line.
10. \v: Vertical tab character. Creates a vertical tab position in some systems.
11. \f: Form feed. Creates a form feed position in some systems.
12. ?: Question mark character. Used to insert a question mark.
13. \ooo: Octal escape character. Represents an ASCII character using its octal value.
14. \xhh: Hexadecimal escape character. Represents an ASCII character using its hexadecimal value.

Escape characters make it easier to work with strings in C and facilitate the inclusion of special characters and formatting in your code.

中文解释：

在C编程中，转义字符是以反斜杠（`\`）开头的特殊字符序列，用于表示一些在字符串中直接包含起来比较困难或不可能的字符，或执行特定的操作。转义字符允许您在字符串中插入特殊字符、控制字符以及其他不可打印字符，还可以执行程序内的某些控制操作。

C语言中常见的转义字符包括：

1. ：换行字符。将光标移至下一行的开头，创建一个换行符。
2. ：制表符。插入一个水平制表位置，用于文本对齐。 `Tab`
3. \：反斜杠字符。用于插入文字中的反斜杠。
4. '：单引号字符。用于插入单引号。
5. "：双引号字符。用于插入双引号，通常用于字符串字面值。
6. \0：空字符。表示C风格字符串的结束。
7. \a：响铃字符。通常会触发系统或控制台的响铃声。
8. \b：退格字符。将光标移至前一个位置，通常用于模拟退格操作。
9. ：回车字符。将光标移至当前行的开头。
10. \v：垂直制表符。在某些系统中创建垂直制表位置。
11. \f：换页字符。在某些系统中创建换页位置。
12. ?：问号字符。用于插入问号。
13. \ooo：八进制转义字符。使用八进制值表示ASCII字符。
14. \xhh：十六进制转义字符。使用十六进制值表示ASCII字符。

转义字符使得在C语言中处理字符串变得更加灵活，并方便了在代码中包含特殊字符和格式化。
