I am trying a basic code snippet on C. My intention is to ignore all characters until 'a' is entered by the user and then copy the next non-whitespace character to x.
#include <stdio.h>
int main()
{
char x ;
printf ("Input character - ") ;
scanf (" a %c", &x) ;
printf ("x=%c is the input", x) ;
return 0 ;
}
I give input in the console as:
a <enter>
s <enter>
I get the output as: x=s is the input. This is correct.
But when I give input in the console as:
b <enter>
The execution finishes suddenly and I get the output as: x= is the input.
I don't understand this behavior. I was expecting the program to ignore 'b' and subsequent characters until 'a' is entered by the user. Did I interpret the scanf statement wrongly?