tags:

views:

100

answers:

3

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 ...

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);
}
+7  A: 

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

RichieHindle
thx, understand
bossgameboy
A: 

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

Didier Trosset
A: 

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 )
codaddict