> 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，循环结束。
