SCANF返回值问题

来源:百度知道 编辑:UC知道 时间:2024/07/05 07:20:24
为什么
int b;
int a;
a=scanf("%d",&b);
输入数字返回1, 输入字母返回0??

返回值 a 表示成功地读到的数的个数。

输入字母,按整数格式%d读,失败了,所以a=0.

Both scanf and wscanf return the number of fields successfully converted and assigned; the return value does not include fields that were read but not assigned. A return value of 0 indicates that no fields were assigned. The return value is EOF for an error or if the end-of-file character or the end-of-string character is encountered in the first attempt to read a character.

输入数字,匹配%d,配置b的值成功,scanf返回成功配置数量1;
输入字母,不匹配%d,配置b的值失败,scanf返回成功配置数量0;
输入Ctrl+Z,scanf返回-1;

scanf() printf()
返回值是这样定义的:
正常下:返回成功输入输出的变量个数
异常下:返回0