From my previous post, I come to know that getchar() completes only when we press Enter. Let's consider this code:
#include<stdio.h>
main()
{
getchar();
getchar();
getchar();
getchar();
getchar();
}
I expected it to run like this: I press some key1 then press Enter, then key2 an Enter, then key3 and Enter, then key4 and Enter and at last key5+Enter, the program should terminate now. This is not what actually happens. What happens is this: I press some key1 then press Enter, then key2 an Enter, then key3 and Enter, the program eventually terminates!
- Why don't the last two getchar() work?
Another weird thing that I observed is that if I do: key1,key2,key3,key4+Enter then the program terminates. E.g. If I press q,w,e and r in succession and then Enter, the program terminates.
- Why not all the getchar() ask for enter? Does this mean that getchar() take any other key as Enter? But then does the next key is taken as the input for the next getchar()?
Let's consider another code:
#include<stdio.h>
main()
{
int c=getchar();
int d=getchar();
int e=getchar();
printf("2 getchar are remaining\n");
int f=getchar();
int g=getchar();
printf(" c is %d, d is %d, e is %d, f is %d and g is %d",c,d,e,f,g);
}
I input: ABCDEFG then Enter. The line 2 getchar are remaining should be printed as soon as I press C or D. But it is printed at last, means that all the getchar()s get executed simultaneously-- this is weird.
- Isn't the program get executed line by line? I.e. after the third getchar, printf() should work. But it works at last when all the getchar()s are executed.