【发布时间】:2021-03-03 20:57:49
【问题描述】:
例如,我如何生成错误消息? 'a' 是输入值?
#include <stdio.h>
int main ( void )
{
int a;
scanf ("%d" , & a) ;
// if a is not a number, then generate error
return 0 ;
}
【问题讨论】:
-
不要使用scanf,使用fgets + strtol
-
必须是scanf...
-
当
a不是整数时是否要生成错误? -
是的,就是这样。
-
检查返回值。
scanf返回扫描的项目数,或文件末尾的 EOF 或其他错误。比如if (scanf ("%d", &a) == EOF) { ... handle error ...}。
标签: c