#Reading character after reading string input

6 messages · Page 1 of 1 (latest)

verbal arch
#

int main(){ char a[10]; char b; printf("enter a\n"); scanf("%s",a); printf("enter b\n"); scanf("%c",&b); printf("%s %c",a,b); return 0; }

After I enter the first line of input which is stored in a, the program exits and doesn't let me take an input for b unless I clear the input buffer by using fflush(stidn). This works fine however if b were an array and I read one string after another without flushing the input buffer. Why the difference between a string input and a character input?

shut sandBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

short steeple
#
scanf(" %c",&b);

Note the space there.

#

That space "eats" all whitespace that remains in the buffer ahead of your input.
So you don't need to flush the buffers.

viral hamlet