Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Second scanf is not working

Tags:

c

i am having trouble with this c language code:

 char st[2];

 printf("enter first value:");
 scanf("%c", &st[0]);

 printf("enter second value:");
 scanf("%c", &st[1]);

So my computer didn't ask me to enter the second value, I mean to say that it only print the first printf statement then I enter a character and then it only prints the second printf statement and program end without taking the second input.

Please help. What's wrong with this code?

-Thanks in advance.

like image 501
Peeyush Avatar asked Oct 26 '10 12:10

Peeyush


2 Answers

Well it did. The character(s) produced by the ENTER key is present in the buffer already.

like image 113
leppie Avatar answered Nov 15 '22 07:11

leppie


I think your problem is the second scanf is receiving the "Enter" key press.

like image 43
nathan Avatar answered Nov 15 '22 07:11

nathan