CODE 1:-
char ch1, ch2;
printf("Input the first character:");
scanf("%c", &ch1);
while(getchar()!='\n');
printf("Input the second character:");
ch2 = getchar();
In this case while(getchar()!='\n');
, clears the effect of enter-key
pressed for first input.
CODE 2:-
char ch1, ch2;
printf("Input the first character:");
scanf("%c", &ch1);
while(getch()!='\n');
printf("Input the second character:");
ch2 = getchar();
In this case while(getch()!='\n');
, do not clears the effect of enter-key
pressed for first input. AND THE LOOP TURNS OUT TO BE INFINITE.
What is the difference in the functioning of getch()
and getchar()
this case?