i am having trouble with this c language code:
char st[2];
printf(\"enter first value:\");
scanf(\"%c\", &st[0]);
printf(\"enter second value:\");
use fflush(stdin);
function before the second scanf();
. It will flush the ENTER key generated after first scanf();.
Actually, your second scanf() is taking the ENTER as its input and since scanf terminates after getting an ENTER, it is not taking anything else by your side.
I think your problem is the second scanf is receiving the "Enter" key press.
Change
scanf("%c", &st[0]);
to this
scanf(" %c", &st[0]);
That's a shotty answer (no error checking or anything) but its quick and easy.
Well it did. The character(s) produced by the ENTER key is present in the buffer already.
You're getting the implicit newline you entered as the second character, i.e. st[1]
is getting the value '\n'
. An easy way to fix this is to include the newline in the expected format string: scanf("%c\n", &st[0]);