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

The getch() function reads a single character from the keyboard. It doesn’t use any buffer, so entered data will not be displayed on the output screen.

The getche() function reads a single character from the keyword, but data is displayed on the output screen. Press Alt+f5 to see the entered character.

In C programming, both getch() and getche() are functions used for reading a single character from the keyboard. However, there’s a difference between them:

  1. getch():
    • getch() is a non-standard function, meaning it’s not defined by ANSI C, but it’s commonly found in many C compilers.
    • It reads a single character from the keyboard but does not echo the character to the screen (i.e., the character typed by the user is not displayed on the screen).
    • It typically does not require the user to press the Enter key after typing the character.
    • It returns the ASCII value of the character read.
  2. getche():
    • getche() is also a non-standard function and not defined by ANSI C, but like getch(), it’s commonly found in many C compilers.
    • Similar to getch(), it reads a single character from the keyboard, but it echoes the character to the screen (i.e., the character typed by the user is displayed on the screen).
    • It also typically does not require the user to press the Enter key after typing the character.
    • It returns the ASCII value of the character read.

So, the main difference lies in whether or not the character typed is echoed to the screen. getche() echoes the character while getch() does not.