What is getch()?
What is getch()
getch() is a function in c programming language. But this function is not present in standard C programming.
Meaning getch() is not a built in function, it is present in conio.h header file.
This header is mostly used by compilers like Turbo C which MS-DOS based.
getch() functions is used to accept a character from user but this function unlike other function in c does not store the charcter, it does not use any buffer.
This function accepts the character and immediately return it without waiting for the enter key.
Function Syntax:
int getch(void)
Usage Syntax:
getch();
As you can tell from the syntax it does not accept any character and it returns the ascii value of the key pressed by the user.
Here is an example program to demonstrate the use of getch() function.
// demonstration of getch() function in C program
#include <stdio.h>
// import the library which contains getch() function
#include <conio.h>
int main()
{
printf("%c", getch());
return 0;
}
Output
Input: a (Without enter key)
Output: Program terminates immediately.
But when you use DOS shell in Turbo C,
it shows a single a, i.e., 'a'
Conclusion is, getch() function does not use buffer, it immediately returns the key pressed, it does not wait for enter key, this function can be used to accept secret codes like atm pin or password.
Comments
Post a Comment