Getchar () in C exits without hitting Enter

From my previous post , I learn that getchar () only completes when we hit Enter. Consider this code:

#include<stdio.h>
main()
{
  getchar();
  getchar();
  getchar();
  getchar();
  getchar();


}

      

I expected it to work like this: I press key1, then press Enter, then key2 a Enter, then key3 and Enter, then key4 and Enter, and finally key5 + Enter, the program should end. This is not what is actually happening. The following happens: I press key1, then press Enter, then key2 a Enter, then key3 and Enter, the program ends!

  • Why don't the last two getchar () functions work?

Another strange thing that I observed is that if I do this: key1, key2, key3, key4 + Enter, then the program exits. For example. If I press q, w, e, and r in sequence and then Enter, the program exits.

  • Why don't all getchar () require input? Does this mean getchar () accepts any other key as Enter? But then the next key is taken as the input for the next getchar ()?

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);

} 

      

Input: ABCDEFG, then Enter. The other 2 lines should be printed as soon as I hit C or D. But it finally prints, means all getchar () s are executed at the same time - it's weird.

  • Does the program start line by line? That is, after the third getchar, printf () should work. But it works finally when all getchar () s are executed.
+3


source to share


4 answers


It is not correct that getchar () exits after pressing enter. getchar () completes whenever there are characters to read. This difference is significant: see, for example, if you are running your program with standard input redirected to a file:

$ hexdump -C abcd_file 
00000000  61 62 63 64 65                                    |abcde|
00000005

$ ./in < abcd_file 
$

      

Note that "abcd_file" is a file containing "abcde", a newline and your program ends without having to use the newline anywhere. This is because the file provides characters all the time and does not wait for a newline.

Generic terminals or terminal emulators, on the other hand, have a mode of operation called "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 wrong "getchar () waits for ENTER" comes from. You can switch your terminal from canonical mode and watch it fetch all characters without having to hit enter:



$ stty -icanon; ./in; stty icanon
ggggg$

      

In this case, 5 characters without input terminated the program execution.

Finally, the reason for getchar () looks like it returns earlier because it also returns ENTER characters. So "a \ nb \ nc \ n" has 6 characters, the first 5 are returned by getchar (), the sixth is removed from the terminal queue after the program finishes. Typing "abcd \ n" also means that getchar () will be immediately available for 5 consecutive reads because there are 5 characters written to the terminal queue.

http://www.gnu.org/software/libc/manual/html_node/Noncanonical-Input.html#Noncanonical-Input

+5


source


For your first question, the enter key is a character that getchar

can handle and return. So if you type two characters and hit enter, you have to call getchar

three times to clear the input buffer. Remember getchar

not to take the key from the keyboard, but from the input buffer. So if you hit five characters into your input buffer for example. abcd enter, you will need to call getchar

five times to get the whole thing, and the first one will not return until you click enter.

This also explains your second question. The call getchar

is executed sequentially, not concurrently as soon as it enterhits, but the first one is not executed until then.



You can read about disabling input buffering .

+2


source


Is the program running line by line? That is, after the third getchar, printf () should work. But it works finally when all getchar () s are executed.

It runs line by line, however the ENTER key is a valid input to getchar () and so it will read its ASCII value. Can't say any more, because this value is system dependent.

+1


source


getchar () takes each character of the input character by character. It doesn't wait for the enter key to be pressed. Run the program below and check the result to get the best image.

 #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: (Input input pressed after inputting 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 set before pressing 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!

0


source







All Articles