首先,你需要知道scanf("%f",&a)这个语句的返回值。scanf的返回值是它读取有效数值的数量,即如果读取到一个合法的数据,就返回1。
此处是读取一个浮点数,如果你输入时输入的是一个字母或非数值型字符,都是无效字符,即不会读取,返回值为0.
scanf("%d %d",&a,&b);
scanf函数定义:
int scanf(const char * restrict format,...);函数返回值为int型。如果a和b都被成功读入,那么scanf的返回值就是2;
如果只有a被成功读入,返回值为1;
如果a和b都未被成功读入,返回值为0;
如果遇到错误或遇到end
of
file,返回值为eof。
就是
如果(未读到合法的浮点数)。
From scanf: On success, the function returns the number of items successfully read. This count can match the expected number of readings or fewer, even zero, if a matching failure happens. In the case of an input failure before any data could be successfully read, EOF is returned.
也就是说,scanf函数执行成功,返回值是等于正确读进来的变量个数;否则,返回的可能是任意不等于正确读进来的变量个数或者EOF。
if (scanf("%f",&a) != 1) {
}
这个语句是说如果scanf函数读进的参数个数不等于1,就执行括号中的代码。