✏️ Explanatory Question

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

👁 1,860 Views
📘 Detailed Answer
🟢 Easy
💡

Answer with Explanation

C Programming Language

What is the Difference Between getch() and getche()?

Both getch() and getche() are keyboard input functions used to read a single character from the keyboard without requiring the user to press the Enter key. The main difference is whether the typed character is displayed on the screen.

Answer:
  • 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 
Important: The 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() and getche() are part of the C Standard Library.
  • Using these functions with modern GCC compilers without checking compiler support.
  • Confusing getch() with getchar().
  • 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 char data type.
  • Basic familiarity with header files such as and .

Interview Questions

  1. What is the difference between getch() and getche()?
  2. Which header file contains getch() and getche()?
  3. Does getch() display the typed character?
  4. Do getch() and getche() require the Enter key?
  5. Are getch() and getche() part of the C Standard Library?
  6. 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.