What is difference between getch and getche? | Sololearn: Learn to code for FREE!


What is difference between getch and getche?

c++ c

9/23/2018 8:23:03 AM

Noorjahan Khatoon

8 Answers

New Answer


getch(): getch() is a nonstandard function and is present in conio.h header file which is mostly used by MS-DOS compilers like Turbo C.  Like above functions, it reads also a single character from keyboard. But it does not use any buffer, so the entered character is immediately returned without waiting for the enter key. Syntax: int getch(); #include <stdio.h> #include <conio.h> int main() { printf("%c", getch()); return 0; } getche(): Like getch(), this is also a non-standard function present in conio.h. It reads a single character from the keyboard and displays immediately on output screen without waiting for enter key. Syntax: int getche(void); #include <stdio.h> #include <conio.h> // Example for getche() in C int main() { printf("%c", getche()); return 0; }


Now, the OP is probably curious about the possible equivalents. getchar() ¹ as the most well-known yet old-school mechanism for processing the stream of character data addresses the lack of those obsolete and nonconforming functions. This function buffers every single inputted character as an "int" value (including control characters and whitespaces like newline (\n), tab (\t), etc when using with a condition as the following example). It probably is seen a lot in standard C reading loop as below (with some simple use cases) int c, count = 0, upper = 0, lower = 0, digit = 0, whitespace = 0; while ( (c = getchar()) != EOF ) { // process each character on the buffer AFTER triggering the EOF by either Ctrl + D (on Linux) or Ctrl + Z (on Windows) // Some useful tasks that can be performed // 1. counting the total # of characters ++count; // 2. counting the upper and lower case letters if (isupper(c)) ++upper; else if (islower(c)) ++lower; // 3. counting digits else if (isdigit(c)) ++digit; // 4. counting whitespaces (including: space, tab, \n, \v, \f, \r) else if (isspace(c)) ++whitespace; } ____ ¹ https://en.cppreference.com/w/c/io/getchar


thanks Satnam Singh


thanks Abhi Varshini




getch() only get a character from keyboard whereas getche() get a character and display it on output screen


getch is the the function that waits for an input from the user. There is nothing like "getche" may be you have seen some functions names or variable names. both are used to take input character but have some difference. getche() give output without any buffer but the getch() give output with buffe https://www.geeksforgeeks.org/difference-getchar-getch-getc-getche/ http://www.cplusplus.com/forum/beginner/105939/