开发者

Why this scanf returns false?

This scanf should always return true until I input none numeric input, but this scanf never executes while loop. Why?

Sample input:

10.0 5.0
Press [Enter] to close the terminal ...

开发者_JS百科Code:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char** argv)
{
    float a, b;
    while ( scanf("%f %f", &a, &b) == 1 )
    {
        printf("%f\n", (a - b) / (a * b));
    }
    return (EXIT_SUCCESS);
}


scanf returns the number of items read, which in this case is 2.


scanf returns the number of items scanned successfully. In your case, it will return 2 to indicate success.


This is because scanf returns 2 which is the number of items successfully read.

To keep reading till an EOF is encountered you can do:

while ( scanf("%f %f", &a, &b) != EOF )
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜