Why does the program work fine when I use only integers? After I insert a number for variable 'a' and then press enter, doesn't \n remain in the buffer? If it indeed remains, then why can I initialize b also? If I had defined b as a char then the b output would b 10; in other words, the null newline character. If, for example, I wanted to enter a name many times for a number of the people using (using what?), would I have any problem? Do I need a function that clears the buffer? So what's wrong and how can I solve this problem
#include <stdio.h>
int main() {
int a,b;
printf("a ");
scanf("%d",&a);
printf("a=%d",a);
printf("\nb ");
scanf("%d",&b);
printf("b=%d",b);
return 0;
}
For most of its conversions, including
%d,scanfstarts by reading and ignoring initial white-space characters, including the new-line character. Only for the conversions with[,c, orndoes it not skip white-space characters.To tell
scanfto skip white-space characters before processing%c, insert a space in the format:scanf(" %c", &b);.