getch in C
Overview
The getch in C is a non-standard function used to receive a character as input from the user. It is defined in the header file conio.h The character entered by the user is not visible on the output screen but is stored in the assigned variable which makes this the best method for receiving passwords from a user.
We can also explain the getch() as a function call that pauses the execution of the program until the user enters a character from the keyboard.
Syntax of getch() in C
The sytax of the getch() function call in C is,
There are no parameters for the function getch in C.
Return Value of getch() in C
The getch() function returns the ASCII value of the character entered by the user. An integer data type will be returned for the ASCII value. (int)
ASCII stands for American Standard Code for Information Interchange and an ASCII value is used to represent characters as numerical values in machines. An ASCII value is 7-bits and can be used to represent 128 characters. For example, character a is represented in ASCII as 97, and character A is represented as 65.
Example
The following example can be used to understand how getch() in C works,
The above code receives the ASCII value of the character entered by the user and stores it in the character_entered variable. Then, prints the character entered using the %c access modifier and its ASCII value using the %d access modifier.
An access modifier is used to represent the data type for a variable. %d denotes int data type and %c denotes char data type.
The Output for the above code will be,
It is important to note that the space next to the line Please enter a character: is blank. This is because, unlike other input functions like scanf(), and gets(), the getch() function will not print or echo the character entered by the user to the screen.
Exceptions of getch() in C
You may have noticed the header conio.h used in the previous example. This header is not part of the C standard library or the POSIX specification.
You can only use this header with an MS-DOS compiler like Turbo C or Borland C. The program provided in the example section will not compile in most online compilers or Linux systems as they use the GCC compiler.
To use the getch() function in programs compiled using the GCC compiler or in Linux systems, we can use the curses.h header which provides many of the same functions as the conio.h header. To use the curses.h header, the ncurses library has to be installed.
The MS-DOS(Microsoft Disk Operating System) is one of the first 32-bit operating systems and hence the turbo C compiler was made for 32-bit systems. This shows that getch() is one of the methods used to get input in old operating systems. The C standard library has many libraries for the C programming language and POSIX(Portable Operating System Interface) is a standard with additional functionalities in addition to those in the standard library.
What is getch() in C
- The getch() in C is a function used in old operating systems to get user input.
- It only accepts one input character at a time or per the function call.
- It pauses the execution of the program until input is provided by the user.
- It doesn't use buffers to store the data and return the data as soon as the user enters it.
- The input entered by the user is not visible on the output screen.
More Examples
The function, getch() in C is mainly used to get passwords or security pins from the user as input. Since the input typed by the user is not printed on the output screen, this forms a good security measure to enter credentials.
We can get a series of characters as input and store them in the character array using the getch() function. The following code illustrates this example,
We have used the * symbol as a marker for entering the password as the data received as input by the getch() function is not visible on the output.
Note: In the character array, the last character of the array is represented with the null character \0. To accommodate this the array always has one extra space.
The Output will be,
We can use the getch() function in C to hold program execution until a character is pressed. The following program illustrates this,
The above code execution will be paused after each question is completed. The code continues execution by pressing a key which marks the next question.
The Output will be,
Conclusion
- The function "getch in c" is used to get a character as input from the user.
- The input provided by the user is not displayed on the output screen.
- Code execution is paused until a character is entered when a getch function is called.
- A getch() function returns the equivalent ASCII value for the character entered.
- The header conio.h in which the getch function is present is not included in the C standard library.