C What is getch()?
Syntax
#include <conio.h>
int getch(void);
Example
#include <stdio.h>
#include <conio.h>
int main() {
char c;
printf("Enter a character: ");
c = getch();
printf("\nYou entered: %c", c);
return 0;
}
Output
Enter a character: a
You entered: a
Explanation
The getch()
function is part of the <conio.h>
header file in C. It is used to read a single character from the keyboard, without echoing the character to the screen.
Use
The getch()
function is particularly useful when writing interactive console programs, such as games or command-line utilities. It can be used to read keyboard input without requiring the user to press the Enter key.
Important Points
- The
getch()
function is a non-standard C library function and is not available on all platforms. - The function reads a single character from the keyboard, without requiring the user to press the Enter key.
- The input is not echoed to the screen.
- The function is often used in conjunction with other console I/O functions, such as
printf()
andscanf()
.
Summary
The getch()
function is a useful tool in C for reading single characters from the keyboard without echoing input to the screen. It is often used in console programs to provide interactive experiences for users. However, it's important to note that it is a non-standard library function and may not be available on all platforms.