> For the complete documentation index, see [llms.txt](https://docs.1l.lol/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.1l.lol/strings/char-array.md).

# 字符数组

### 字符数组基础

字符数组用于存放一串字符，以 `'\0'` 结尾：

```cpp
char s[10] = "hello";     // 5 个字符 + 结束符
char t[] = {'a', 'b', 'c', '\0'};
```

### 字符与数字的转换（重点）

数字字符与其数值相差 `'0'`（即 48）：

```cpp
char ch = '5';
int num = ch - '0';        // 5
int num2 = ch - 48;        // 等价写法

int x = 7;
char c = x + '0';          // '7'
```

大写字母与小写字母相差 32：

```cpp
char up = 'A' + 32;        // 'a'
char lo = 'a' - 32;        // 'A'
```

### 输入输出

```cpp
char s[100];
cin >> s;                  // 遇空格停止
cout << s;                 // 输出到 \0 为止
```

### 常见操作

```cpp
int n = strlen(s);         // 长度
s[0] = 'H';                // 修改某字符
strcpy(a, s);              // 复制
strcat(a, s);              // 拼接
```

### 与 string 的选择

* 需要字符数组 / 与 C 函数配合时用 `char[]`。
* 日常处理字符串优先用 `string`，更方便安全。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.1l.lol/strings/char-array.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
