c语言中的scanf的返回值
时间: 2023-11-27 09:48:14 浏览: 98
在C语言中,scanf函数的返回值与正确输入数据的个数有关。当输入有错误时,错误输入之前正确输入数据的个数即为scanf的返回值。简而言之,scanf返回值等于读取到正确数据的个数。如果输入流已经结束,则返回EOF(End Of File)。例如,如果使用scanf("%d %d",&a,&b)来读取两个整数,正确的输入应该是两个整数中间用空格分开,如果输入的是非整数字符或者输入流已经结束,那么返回值将会小于2。\[1\]
举个例子,如果使用以下代码:
```c
#include <stdio.h>
int main() {
int a, b, c;
int result = scanf("%d %d %d", &a, &b, &c);
printf("%d", result);
return 0;
}
```
如果输入的是三个整数,那么返回值将会是3。但如果输入的是非整数字符或者输入流已经结束,那么返回值将会小于3。\[2\]
另外,scanf函数也可以只输入一个参数,例如scanf("%d", &a),这样就只会读取一个整数给变量a。\[3\]
#### 引用[.reference_title]
- *1* [scanf函数的返回值是什么?](https://blog.csdn.net/weixin_74269833/article/details/127402262)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [C语言中scanf的返回值](https://blog.csdn.net/weixin_45729253/article/details/102848427)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
- *3* [C语言小知识 之 scanf的返回值](https://blog.csdn.net/C_Little_Noob/article/details/118603505)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文