- 2071 Shrawan · 1 mark
Syntax and example of getche()
Short answer
getche() reads a single character from keyboard, echoes it, and does not wait for Enter. It requires <conio.h> and is used for immediate input like menu choices. Example: char ch = getche();
Write syntax, example and use of getche().
getche() reads one character from the keyboard and echoes it on the screen, without waiting for the Enter key. It is declared in <conio.h> (Turbo C / Windows compilers; not in gcc). The "e" stands for echo.
Syntax: ch = getche();
Example (Turbo C):
char ch;
printf("Enter a letter: ");
ch = getche(); /* character is shown as typed */
printf("\nYou typed %c", ch);
Use: to read a single-key response (such as Y/N or a menu choice) immediately, with the typed key visible to the user. In standard C, getchar() does a similar job but waits for Enter:
#include <stdio.h>
int main(void)
{
char ch;
printf("Press a key: ");
ch = getchar(); /* portable counterpart of getch()/getche() */
printf("You pressed: %c\n", ch);
return 0;
}
Output for the key k:
Press a key: You pressed: k
✓ Answer checked by Claude Opus 5.5 (high effort)