I was recently running a c program in my PC. It have a for loop in which some char d is scanned. The for loop runs for 3 times. During each running it prints the the count of running and then scans the value of char d. The program is as follows
#include<stdio.h>
int main(){
int f;
char d;
for(f=0;f<3;f++){
printf("Choice %d\n", f);
scanf("%c", &d);
}
return 0;
}
Now the trouble is that when I run the program, the for skips the scanf part when f is 1. Now if i changed the code as follows
#include<stdio.h>
int main(){
int f;
int d;
for(f=0;f<3;f++){
printf("Choice %d\n", f);
scanf("%d", &d);
}
return 0;
}
Now the program works fine. and scanf is executed for every iteration of for loop.
What does seem to be the problem here? I mean when d is of type int it works fine, but when d is of type char it does not work correctly.