What is the difference between getch() and getchar()?

55,044

Solution 1

getchar() is a standard function that gets a character from the stdin.

getch() is non-standard. It gets a character from the keyboard (which may be different from stdin) and does not echo it.

Solution 2

The Standard C function is is getchar(), declared in <stdio.h>. It has existed basically since the dawn of time. It reads one character from standard input (stdin), which is typically the user's keyboard, unless it has been redirected (for example via the shell input redirection character <, or a pipe).

getch() and getche() are old MS-DOS functions, declared in <conio.h>, and still popular on Windows systems. They are not Standard C functions; they do not exist on all systems. getch reads one keystroke from the keyboard immediately, without waiting for the user to hit the Return key, and without echoing the keystroke. getche is the same, except that it does echo. As far as I know, getch and getche always read from the keyboard; they are not affected by input redirection.

The question naturally arises, if getchar is the standard function, how do you use it to read one character without waiting for the Return key, or without echoing? And the answers to those questions are at least a little bit complicated. (In fact, they're complicated enough that I suspect they explain the enduring popularity of getch and getche, which if nothing else are very easy to use.)

And the answer is that getchar has no control over details like echoing and input buffering -- as far as C is concerned, those are lower-level, system-dependent issues.

But it is useful to understand the basic input model which getchar assumes. Confusingly, there are typically two different levels of buffering.

  1. As the user types keys on the keyboard, they are read by the operating system's terminal driver. Typically, in its default mode, the terminal driver echoes keystrokes immediately as they are typed (so the user can see what they are typing). Typically, in its default mode, the terminal driver also supports some amount of line editing -- for example, the user can hit the Delete or Backspace key to delete an accidentally-typed character. In order to support line editing, the terminal driver is typically collecting characters in an input buffer. Only when the user hits Return are the contents of that buffer made available to the calling program. (This level of buffering is present only if standard input is in fact a keyboard or other serial device. If standard input has been redirected to a file or pipe, the terminal driver is not in effect and this level of buffering does not apply.)

  2. The stdio package reads characters from the operating system into its own input buffer. getchar simply fetches the next character from that buffer. When the buffer is empty, the stdio package attempts to refill it by reading more characters from the operating system.

So, if we trace what happens starting when a program calls getchar for the first time: stdio discovers that its input buffer is empty, so it tries to read some characters from the operating system, but there aren't any characters available yet, so the read call blocks. Meanwhile, the user may be typing some characters, which are accumulating in the terminal driver's input buffer, but the user hasn't hit Return yet. Finally, the user hits Return, and the blocked read call returns, returning a whole line's worth of characters to stdio, which uses them to fill its input buffer, out of which it then returns the first one to that initial call to getchar, which has been patiently waiting all this time. (And then if the program calls getchar a second or third time, there probably are some more characters -- the next characters on the line the user typed -- available in stdio's input buffer for getchar to return immediately. For a bit more on this, see section 6.2 of these C course notes.)

But in all of this, as you can see, getchar and the stdio package have no control over details like echoing or input line editing, because those are handled earlier, at a lower level, in the terminal driver, in step 1.

So, at least under Unix-like operating systems, if you want to read a character without waiting for the Return key, or control whether characters are echoed or not, you do that by adjusting the behavior of the terminal driver. The details vary, but there's a way to turn echo on and off, and a way (actually a couple of ways) to turn input line editing on and off. (For at least some of those details, see this SO question, or question 19.1 in the old C FAQ list.)

When input line editing is turned off, the operating system can return characters immediately (without waiting for the Return key), because in that case it doesn't have to worry that the user might have typed a wrong keystroke that needs to be "taken back" with the Delete or Backspace key. (But by the same token, when a program turns off input line editing in the terminal driver, if it wants to let the user correct mistakes, it must implement its own editing, because it is going to see --- that is, successive calls to getchar are going to return -- both the user's wrong character(s) and the character code for the Delete or Backspace key.)

Solution 3

getch() it just gets an input but never display that as an output on the screen despite of us pressing an enter key.

getchar() it gets an input and display it on the screen when we press the enter key.

Solution 4

  • getchar is standard C, found in stdio.h. It reads one character from stdin(the standard input stream = console input on most systems). It is a blocking call, since it requires the user to type a character then press enter. It echoes user input to the screen.

  • getc(stdin) is 100% equivalent to getchar, except it can also be use for other input streams.

  • getch is non-standard, typically found in the old obsolete MS DOS header conio.h. It works just like getchar except it isn't blocking after the first keystroke, it allows the program to continue without the user pressing enter. It does not echo input to the screen.

  • getche is the same as getch, also non-standard, but it echoes input to the screen.

Share:
55,044
bubble
Author by

bubble

I am a bubble right now and novice. But I promise to help community as I grow stronger. Find my blog here

Updated on September 16, 2020

Comments

  • bubble
    bubble over 3 years

    What is the exact difference between the getch and getchar functions?

  • Ryan Haining
    Ryan Haining almost 10 years
    getchar() returns the character but doesn't display it. that's up to the programmer to do. getchar() can read from stdin before enter is pressed if the input isn't cooked.
  • Rishi Shukla
    Rishi Shukla over 7 years
    Sir, could you please point me to the resource(s) or explain the statement "which may be different from stdin" with an example ? Thank you !
  • Roy Dictus
    Roy Dictus over 7 years
    Meaning that stdin may be the keyboard, or it may be another input stream.
  • Steve Summit
    Steve Summit almost 6 years
    I believe getch blocks until one keystroke is pressed, it just doesn't block waiting for Return at the end of the line.
  • Lundin
    Lundin almost 6 years
    @SteveSummit Ah yeah I got it mixed up with another oldie called kbhit. Fixed.
  • This isn't my real name
    This isn't my real name almost 6 years
    Any pointers into how to adjust the behavior of the terminal driver?
  • Steve Summit
    Steve Summit almost 6 years
    @Thisisn'tmyrealname All the gory details: No. Pointers: Good suggestion; added.