Reputation: 7587
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.
Upvotes: 4
Views: 6901
Reputation: 3
getchar() takes every input character by character. It does not wait for the enter key to be pressed. Run the program given below and check the output to get a better picture.
#include <stdio.h>
int main( ) {
int c;
int i;
printf( "Enter a value :");
i = getchar();
printf( "Enter a value :");
c = getchar();
printf( "\nYou entered: ");
putchar( c );
printf( "\nYou entered: ");
putchar(i);
return 0;
}
OUTPUT: (Enter key pressed after entering one character)
Enter a value :1
Enter a value :
You entered:
You entered: 1
Process returned 0 (0x0) execution time : 3.183 s
Press any key to continue.
OUTPUT: (INPUTS given before pressing the enter key)
Enter a value :12
Enter a value :
You entered: 2
You entered: 1
Process returned 0 (0x0) execution time : 5.389 s
Press any key to continue.
Hope this helps!
Upvotes: 0
Reputation: 8030
It's not true that getchar() completes after you press enter. getchar() completes whenever there are characters to read. This difference is significant: see, for example, if you use your program with the standard input redirected to a file:
$ hexdump -C abcd_file
00000000 61 62 63 64 65 |abcde|
00000005
$ ./in < abcd_file
$
Notice that "abcd_file" is a file that contains "abcde", no newline and your program finishes without requiring a newline anywhere. That's because the file is providing characters all the time and not waiting for a newline.
Common terminals or terminal emulators, on the other hand, have an operation mode that is called the "canonical mode". Canonical mode means that the terminal supports "command line processing facilities" and will not signal an available character until the user presses ENTER. That's where the incorrect "getchar() waits for ENTER" story comes from. You can switch your terminal out of canonical mode and see it retrieving all the characters without the need for pressing enter:
$ stty -icanon; ./in; stty icanon
ggggg$
In this case 5 characters without an enter made the program to finish.
Finally, the reason for getchar() look like it is returning early is because it also returns the ENTER characters. So "a\nb\nc\n" is 6 characters, the first 5 are returned by getchar(), the sixth is deleted from the terminal queue after the program finishes. Typing "abcd\n" also means that getchar() will be immediatelly available for 5 consecutive reads, because there are 5 characters stored in the terminal queue.
http://www.gnu.org/software/libc/manual/html_node/Noncanonical-Input.html#Noncanonical-Input
Upvotes: 7
Reputation: 447
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.
It is executed line by line, the ENTER key however is valid input for getchar() and so it will read its ASCII value. Can't tell you more because this value varies between systems.
Upvotes: 1
Reputation: 9006
For your first question, the enter key is a character that getchar
can process and return. So if you type two characters and press enter, you have to call getchar
three times to clear out your input buffer. Remember, getchar
doesn't take a key from the keyboard, but from the input buffer. So if you push five characters into your input buffer, e.g. abcdenter, you'll have to call getchar
five times to get all of that, and the first one won't return until you hit enter.
That also explains your second question. The getchar
calls are executing sequentially, not simultaneously, once enter is hit, but the first one doesn't execute until then.
You may want to read about disabling input buffering.
Upvotes: 2