getch()reads a single character from the keyboard without displaying (echoing) it on the screen.getche()reads a single character from the keyboard and displays (echoes) it on the screen.
Header File
Both getch() and getche() are declared in the
header file.
#include
header file is not a part of the C Standard Library.
It is supported by older DOS/Turbo C compilers and some Windows compilers but is
generally not available in modern GCC or Clang compilers.
Example 1: Using getch()
#include
#include
int main()
{
char ch;
printf("Press any key: ");
ch = getch();
printf("\nYou pressed: %c", ch);
return 0;
}
Sample Output
Press any key:
You pressed: A
Notice that when the user presses A, it is not displayed immediately after "Press any key:".
Example 2: Using getche()
#include
#include
int main()
{
char ch;
printf("Press any key: ");
ch = getche();
printf("\nYou pressed: %c", ch);
return 0;
}
Sample Output
Press any key: A
You pressed: A
Here, the character A is immediately displayed on the screen
because getche() echoes the typed character.
Difference Between getch() and getche()
| Feature | getch() |
getche() |
|---|---|---|
| Displays Typed Character | No | Yes |
| Echoes Input | No | Yes |
| Requires Enter Key | No | No |
| Reads | One Character | One Character |
| Header File | |
|
| Standard C Function | No | No |
| Common Use | Password input, pause screen | Normal single-character input |
Working Flow
User Presses a Key
│
▼
getch()
│
├── Character is NOT displayed
▼
Program Receives Character
User Presses a Key
│
▼
getche()
│
├── Character IS displayed
▼
Program Receives Character
Advantages
getch()
- Does not display typed characters.
- Useful for password entry.
- Commonly used to pause program execution.
getche()
- Displays the entered character.
- Useful when the user should see what was typed.
- Suitable for simple keyboard input.
Common Mistakes
Avoid These Mistakes
- Assuming
getch()andgetche()are part of the C Standard Library. - Using these functions with modern GCC compilers without checking compiler support.
- Confusing
getch()withgetchar(). - Expecting either function to read an entire string.
Best Practices
- Use
getchar()for standard C programs. - Use
getch()only when compiler support is available. - Prefer portable functions for cross-platform applications.
- Use
getch()mainly in legacy Turbo C programs.
Prerequisites
Before Learning This Topic
- Basic understanding of input and output functions in C.
- Knowledge of characters and the
chardata type. - Basic familiarity with header files such as
and.
Interview Questions
- What is the difference between
getch()andgetche()? - Which header file contains
getch()andgetche()? - Does
getch()display the typed character? - Do
getch()andgetche()require the Enter key? - Are
getch()andgetche()part of the C Standard Library? - What is the standard alternative to
getch()in modern C programming?
Key Takeaway
Both getch() and getche() are non-standard C
functions used to read a single character without pressing the Enter key.
The primary difference is that getch() does not
display the typed character, whereas getche() does.
Since these functions belong to , they are mainly
used with older compilers such as Turbo C and are generally unavailable in
modern standard C environments.