I have written two programs. In the first one I'm not using getchar to take a character from keyboard, in this case, the compilation is completely missing the second scanf. So to overcome this I have used getchar. In this case I'm successfully able to give input but comparison is not happening. Though I have given input as "d" and "d" output is "bye" only.
#include<stdio.h>
main(){
char c,f;
printf("e");
scanf("%c",&c);
printf("one more");
scanf("%c",&f);
if(c=='d'&&f=='d')
printf("hi");
else
printf("bye");
}
with getchar
#include<stdio.h>
main(){
char c,f;
printf("e");
scanf("%c",&c);
printf("one more");
scanf("%c",&f);
getchar();
if(c=='d'&&f=='d')
printf("hi");
else
printf("bye");
}
The new line character will remain in standard input as it will not be consumed by the scanf("%c")
. This means the second scanf() reads the newline charcacter, and not the next input. Changing to scanf(" %c")
would be a solution, which will skip leading white space.