What is getch() in C Language
Categories: C language
The getch() is a predefined non-standard function that is defined in conio.h header file. It is mostly used by the Dev C/C++, MS- DOS's compilers like Turbo C to hold the screen until the user passes a single value to exit from the console screen. It can also be used to read a single byte character or string from the keyboard and then print. It does not hold any parameters. It has no buffer area to store the input character in a program.
Why we use a getch() function?
We use a getch() function in a C/ C++ program to hold the output screen for some time until the user passes a key from the keyboard to exit the console screen. Using getch() function, we can hide the input character provided by the users in the ATM PIN, password, etc.
Syntax:
int getch(void);
Parameters: The getch() function does not accept any parameter from the user.
Return value: It returns the ASCII value of the key pressed by the user as an input.
Write a simple program to display the character entered by the user using the getch() function in C.
Program.c
#include <stdio.h>
#include <conio.h>
int main()
{
printf(" Passed value by the User is: %c", getch()); // print a character entered by the user
return 0;
}
Output:
Passed value by the User is: P
Write a simple program to hold the console screen until the user gives a key to exit.
Program2.c
#include <stdio.h>
#include <conio.h>
int main()
{
printf("Enter a key to exit the console screen.\n");
getch();
return 0;
}
Output:
Enter a key to exit the console screen.