What is the Difference Between getch and getche?
🆚 Go to Comparative Table 🆚The main difference between getch
and getche
is that getch
reads a single character from the keyboard without displaying it on the screen, while getche
reads a single character from the keyboard and immediately displays it on the screen. Both functions are non-standard and are typically found in the conio.h
header file. Here are the key differences:
- getch: This function reads a single character from the keyboard without using any buffer, so the entered character is returned immediately without waiting for the enter key. It is useful for situations where you want to read input without displaying it on the screen.
- getche: This function reads a single character from the keyboard and displays it immediately on the output screen, without waiting for the enter key. It is useful for situations where you want to read input and display it on the screen in real-time.
In summary, getch
is used for reading input without displaying it, while getche
is used for reading input and displaying it immediately on the screen.
Comparative Table: getch vs getche
Here is a table comparing the getch()
and getche()
functions:
Function | Description | Echoes Character | Usage |
---|---|---|---|
getch() |
Reads a single character from the keyboard without using any buffer, immediately returns the character without waiting for the Enter key to be pressed. | No | Nonstandard function, mostly used by MS-DOS compilers like Turbo C. |
getche() |
Reads a single character from the keyboard and displays the character on the screen. | Yes | Nonstandard function, used in the <conio.h> header file. |
Both getch()
and getche()
are nonstandard functions, not part of the standard libraries and not defined by POSIX. They are used to receive input from users and are present in the conio.h
header file.