getch() is a nonstandard function and is present in conio.h header file which is mostly used by MS-DOS compilers like Turbo C. It is not part of the C standard library or ISO C, nor is it defined by POSIX.
Like these functions, getch() also reads a single character from the keyboard. But it does not use any buffer, so the entered character is immediately returned without waiting for the enter key.
Parameters: This method does not accept any parameters.
Return value: This method returns the ASCII value of the key pressed.
Input: g (Without enter key) Output: Program terminates immediately. But when you use DOS shell in Turbo C, it shows a single g, i.e., 'g'
Important Points regarding getch() method:
- getch() method pauses the Output Console untill a key is pressed.
- It does not use any buffer to store the input character.
- The entered character is immediately returned without waiting for the enter key.
- The entered character does not show up on the console.
- The getch() method can be used to accept hidden inputs like password, ATM pin numbers, etc.
Example: To accept hidden passwords using getch()
Note: Below code won’t run on Online compilers, but on MS-DOS compilers like Turbo IDE.
Enter Password: ******** Entered password: Abcd1234
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Difference between getc(), getchar(), getch() and getche()
- btowc() function in C/C++ with Examples
- wcsspn() function in C/C++ with Examples
- getdate() and setdate() function in C with Examples
- time.h localtime() function in C with Examples
- C fopen() function with Examples
- How to call function within function in C or C++
- Difference Between malloc() and calloc() with Examples
- Understanding "volatile" qualifier in C | Set 2 (Examples)
- isalpha() and isdigit() functions in C with cstring examples.
- Pointers in C/C++ with Examples
- find command in Linux with examples
- Understanding Lvalues, PRvalues and Xvalues in C/C++ with Examples
- Logical Not ! operator in C with Examples
- iswprint() in C/C++ with Examples
- iswgraph() in C/C++ with Examples
- mbrtoc16() in C/C++ with Examples
- mbrtoc32() in C/C++ with Examples
- wmemset() in C/C++ with Examples
- Types of Literals in C/C++ with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.