Table of contents
1.
Introduction
2.
Conio.h Header File
3.
getch() function
3.1.
Syntax of getch() function:
3.2.
Example of getch()
3.3.
C
4.
getche() function
4.1.
Example of getche()
4.2.
C
5.
What are the Similarities Between getch and getche?
6.
Difference between getch and getche
7.
Frequently Asked Questions
7.1.
Which header file is used for getche()?
7.2.
Is conio.h in C a useful header file?
7.3.
Is getch() used to take user input?
7.4.
Can we substitute getch for a return statement?
8.
Conclusion
Last Updated: Jul 3, 2024
Easy

Difference between getch and getche

Author Nidhi Kumari
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Numerous input and output functions are available in the C programming language. Getch and Getche are two examples of such functions. The getch and getche are non-standard functions(not a part of standard libraries) in conio.h header file. These functions are used to receive input from users and return an integer value. 

Difference between getch and getche

Although these functions appear to be the same, there are some differences among them. In this article, we will cover the difference between getch and getche. Let’s first discuss about conio.h header file and what are getch and getche functions with examples, then we will move to the difference between getch() and getche().

Also Read : C Static Function, Tribonacci Series

Conio.h Header File

The term "header file" refers to a file with the .h extension that contains shared C function declarations and macro definitions. ‘Conio’ stands for console input-output, and ‘h’ represents header files. Each compiler has a different set of functions that are declared inside the conio.h file.

Some of the popular functions of conio.h in C are as follows:

  • clrscr()
  • getch()
  • putch()
  • getche()
  • cscanf()
  • cprintf() etc.
     

Here we will mainly discuss getch() and getche() functions and the difference between getch and getche functions.

You can also read about, loop and while loop

getch() function

It is a non-standard function in C programing language to receive character input from the user. The user-entered character is saved in the designated variable but is not displayed on the output screen. In other words, we use the getch() function to hold the output screen until the user presses a key on the keyboard to close the console window. 

 It is mostly used by MS-DOS compiles, for example, turbo C.

Syntax of getch() function:

int getch()
You can also try this code with Online C Compiler
Run Code

 

Return value:

The return value is an integer datatype. The user-entered character's ASCII value is returned by the getch() method.

 

Use of getch() function:

We can hide the user’s input characters. For example, passwords, ATM PINs, etc. 

Example of getch()

Here is an example of the getch() function:

  • C

C

#include <stdio.h>
#include <conio.h>


int main() {
   printf("Example of getch function. ");
   char c = getch();
// we are typecasting the value to char.
   printf("The received Input: %c\n", c);
   return 0;
}
You can also try this code with Online C Compiler
Run Code

 

Output:

Example of getch function.
The received Input: c Press any key to continue.
You can also try this code with Online C Compiler
Run Code

getche() function

The getche() function has the ability to read one character from user input and display it immediately on the output display without waiting for the enter key.

Syntax of getch() function:

char_variable = int getche(void);
You can also try this code with Online C Compiler
Run Code

 

We are passing void as parameters, which means there will be no parameters in the getche() function.

Return value:

The echo of the input character.

 

Use of getche() function:

To echo the available keystroke from the console.

Example of getche()

Here is an example of the getche() function to understand the difference between getch and getche.

  • C

C

#include<stdio.h>
#include<conio.h>
int main() {
 char n;
 printf(“Enter any key: ”);
 n = getche();
 printf(“\n The entered character is: % c”, n);
 return 0;
}
You can also try this code with Online C Compiler
Run Code

 

Output:

Enter any key: p
The entered character is: p Press any key to continue.
You can also try this code with Online C Compiler
Run Code


Also see, Short int in C Programming

What are the Similarities Between getch and getche?

The similarities between getch and getche functions are given below:

  • Functionality: Both functions are used to read a single character from the keyboard.
     
  • Header File: They are both declared in the <conio.h> header file in C.
     
  • No Echo: Neither function echoes the character typed by the user to the screen.
     
  • Immediate Input: Both functions do not require the user to press Enter after typing a character.
     
  • Return Type: They both return an integer value representing the ASCII code of the character read from the keyboard.

Difference between getch and getche

The following table shows the difference between getch and getche.

Key points getch getche
Syntax int getch(void); int getche(void);
Display Output On pressing the enter key, it simply shows the entered character. It immediately shows the input character without even pressing the enter key.
Enter Key Enter key is required to display output. Enter key is not required to display output.
Buffer Buffer is not used. Buffer is used.
Hide It can be used to hide data. Such as passwords. It cannot be used to hide data.
Echo It does not echo the input. It echoes the input. That means it displays the input twice when it is being entered using the keyboard and after saving it.

Also read - Bit stuffing program in c

Frequently Asked Questions

Which header file is used for getche()?

The conio.h header file is used for getche. The syntax to add the conio.h header file in your code: #include<conio.h>. Similarly, for the getch() function also, you need to include the conio.h header file.

Is conio.h in C a useful header file?

The conio.h header file is a non-standard header file useful for input-output form console. You must include it if you use any of the following functions: clrscr(), getch(), getche(), kbhit(), putch(), cputs(), etc because all of these library functions are specified inside of the conio.h header file.

Is getch() used to take user input?

Yes, the getch () function takes character input from the user. The output screen does not show any of the user's input. When a getch() function is invoked, code execution is suspended until a character is entered.

Can we substitute getch for a return statement?

It’s not a good practice to use getch instead of return statements. One of the important reasons is the waste of memory space. While return 0 uses no memory, getch() consumes memory.

Conclusion

This article extensively discussed the difference between getch and getche functions in C. We also discussed the syntax, uses, and examples of getch and getche functions.

We hope this blog has helped you. We recommend you visit our articles on different topics of C, such as

If you liked our article, do upvote our article and help other ninjas grow.  You can refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingSystem Design, and many more!

Head over to our practice platform Coding Ninjas Studio to practise top problems, attempt mock tests, read interview experiences and interview bundles, follow guided paths for placement preparations, and much more!!

Happy Reading!!

Live masterclass