The functions getc(), getchar(), getch(), and getche() are used to read a single character in C. They differ in the input source, buffering behavior, and whether the typed character is displayed on the screen.
- getc() and getchar() are standard input functions defined in <stdio.h>.
- getch() and getche() are non-standard functions available in <conio.h>.
- They differ in buffering and character echo behavior.
getc() Function
The getc() function reads a single character from a given input stream and returns the corresponding integer value (typically ASCII value of read character) on success. It returns EOF on failure.
#include <stdio.h>
int main() {
// Printing character read by getc()
// from standard input stream
printf("%c", getc(stdin));
return 0;
}
Input
g (press enter key)Output
gExplanation: In this program, the getc() function reads a single character from the standard input (stdin). The entered character is immediately echoed to the screen and printed using printf().
getchar() Function
The getchar() function reads a single input character from standard input. So getchar() is equivalent to getc(stdin).
#include <stdio.h>
int main() {
// Printing character read by getchar()
// from standard input stream
printf("%c", getchar());
return 0;
}
Input
g (press enter key)Output
gExplanation: In this program, the getchar() function reads a single character from the standard input (stdin). The character is then printed using printf(), and it is echoed to the screen during input.
getch() Function
getch() is a non-standard library function that reads a single character from the keyboard. But it does not use any buffer, so the entered character does not display on the screen and is immediately returned without waiting for the enter key.
#include <conio.h>
#include <stdio.h>
int main() {
// Printing character read by getch()
// from standard input stream
printf("%c", getch());
return 0;
}
Input
(Just press g key)Output
gExplanation: The getch() function reads a character from the keyboard without displaying it on the screen. The character is then printed using printf(). Since getch() does not echo input, it remains hidden while typing.
getche() Function
getche() function reads a single character from the keyboard and displays immediately on the output screen without waiting for enter key. Like getch(), it is also a non-standard function present in <conio.h> header file.
#include <conio.h>
#include <stdio.h>
int main() {
// Printing character read by getche()
// from standard input stream
printf("%c", getche());
return 0;
}
Input
g (Just press g key)Output
gExplanation: In this program, the getche() function reads a single character from the keyboard and immediately echoes it to the screen. The character is then printed using printf(), and it appears as the user types it.
Difference Between getc(), getchar(), getch(), and getche()
| Function | Header File | Purpose | Input Source | Buffering | Return Type |
|---|---|---|---|---|---|
| getc() | <stdio.h> | Reads a character from a given file. | File stream | Buffered | int (ASCII value of the character) |
| getchar() | <stdio.h> | Reads a character from standard input. | Standard input (stdin) | Buffered | int (ASCII value of the character) |
| getch() | <conio.h> | Reads a single character from keyboard. | Keyboard | Unbuffered | char (character entered) |
| getche() | <conio.h> | Reads a single character from keyboard. | Keyboard | Unbuffered | char (character entered) |