scanf() not taking input from console [closed]

In your code, please change

scanf("%f",&a);
scanf("%c",&op);
scanf("%f",&b);
scanf("%f",&r);

to

scanf("%f",&a);
scanf(" %c",&op);  //notice here
scanf("%f",&b);
scanf("%f",&r);

Without the leading space before %c, the \n stoted by previous ENTER key press after previous input, will be read and considered as valid input for %c format specifier. So, the second scanf() will not ask for seperate user input and the flow will continue to the third scanf().

The whitespace before %c will consume all leading whitespace like chars, including the \n stoted by previous ENTER key press and will consider only a non-whitespace input.

Note:

  1. %f format specifier reads and ignores the leading \n, so in that case, you don’t need to provide the leading space before %f explicitly.

  2. The signature of main() is int main(void).

Leave a Comment