> 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/basics/io.md).

# 输入输出语句

### cout / cin

```cpp
#include <iostream>
using namespace std;

int main() {
    int a, b;
    cin >> a >> b;
    cout << a + b << endl;
    return 0;
}
```

* `>>` 是**提取运算符**，`<<` 是**插入运算符**。
* `endl` 表示换行并刷新缓冲区，也可用 `"\n"`。

### scanf / printf（C 风格）

速度更快，格式更精细，需要 `#include <cstdio>`。

```cpp
#include <cstdio>

int main() {
    int a; double x;
    scanf("%d%lf", &a, &x);
    printf("%d %.2f\n", a, x);
    return 0;
}
```

常用格式符：

| 类型             | 格式符          |
| -------------- | ------------ |
| int            | `%d`         |
| long long      | `%lld`       |
| float / double | `%f` / `%lf` |
| char           | `%c`         |
| 字符串            | `%s`         |

### 格式控制

* `%.2f`：保留两位小数
* `%5d`：宽度为 5，右对齐
* `%05d`：宽度 5，不足补 0

### 注意

* `.2f` 只做**四舍五入显示**，原值不变。
* scanf 中变量要加取地址符 `&`（字符串数组除外）。


---

# 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/basics/io.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.
