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 )
精彩评论