> For the complete documentation index, see [llms.txt](https://blog.mcyou.cc/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://blog.mcyou.cc/za-xiang/c-yu-yan-scanf-de-fan-hui-zhi.md).

# c语言 scanf的返回值

***

之前做学校的题，题目要求输入一串字符串，没有结尾标识，因此需要单独判断语句是否输入结束。除了正常的方法读`\0`、读或者使用`%s`之外，其实也可以利用scanf的返回值来完成。在用vs调代码之前都不知道scanf有返回值（说你呢不用`scanf_s`不给过的\*\*IDE）

scanf的返回值：

> scanf的返回值是所输入的数据与格式字符串中匹配的次数。
>
> 如果输入出错，则返回EOF（常量，EOF==-1）

例子

```c
	char a[1000];
    char now;
    for(int i =1;scanf("%c",&now)==1;i++){
        a[i]=now;
    }
```

其中利用scanf的返回值做判断条件，只要输入完成，则scanf返回值为0，循环结束。


---

# 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 dynamically by asking a question.

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

```
GET https://blog.mcyou.cc/za-xiang/c-yu-yan-scanf-de-fan-hui-zhi.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
