1.检测用户错误输入
根据scanf()的返回值判断scanf()是否成功读入了指定的数据项数,使程序在用户输入123a时,能输出如下运行结果:
123a↙
Input error!
#include <stdio.h>
int main()
{
int a, b,c;
c=scanf("%d %d", &a, &b);
if (c== 2)
{
printf("a = %d, b = %d\n", a, b);
}
else
{
printf("Input error!\n");
}
return 0;
}//scanf的返回值