Difference between getc(), getchar(), getch() and getche()
Last Updated :
07 Jan, 2025
In C, getc(), getchar(), getch(), and getche() are all functions that is used to read input character in different contexts. Although these functions are used for similar purposes and have similar names, they have different behaviours and use cases.
The below table lists the primary differences between the getc(), getchar(), getch() and getche() in C:
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) |
---|
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.
Example:
C
#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
g
Explanation: 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).
Example:
C
#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
g
Explanation: 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.
Example:
C
#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
g
Explanation: In this program, the getch() function reads a single character from the keyboard without displaying it on the screen. The character is then printed using printf(). Since getch() doesn't echo input, the character won't appear as the user types it.
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.
Example:
C
#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
g
Explanation: 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.
Similar Reads
Basics of File Handling in C File handling in C is the process in which we create, open, read, write, and close operations on a file. C language provides different functions such as fopen(), fwrite(), fread(), fseek(), fprintf(), etc. to perform input, output, and many different C file operations in our program.Need of File Han
13 min read
C fopen() Function In C, the fopen() function is used to open a file in the specified mode. The function returns a file pointer (FILE *) which is used to perform further operations on the file, such as reading from or writing to it. If the file exists then the fopen() function opens the particular file else a new file
5 min read
EOF, getc() and feof() in C In this article, we will discuss the EOF, getc() function and feof() function in C.What is EOF?In C, EOF is a constant macro defined in the <stdlib.h> header file that is used to denote the end of the file in C file handling. It is used by various file reading functions such as fread(), gets()
3 min read
fgets() in C In C, fgets() is a built-in function defined in <stdio.h> header file. It reads the given number of characters of a line from the input stream and stores it into the specified string. This function stops reading the characters when it encounters a newline character, has read the given number o
3 min read
fprintf() in C fprintf is used to print content in file instead of stdout console. int fprintf(FILE *fptr, const char *str, ...); Example: C // C Program for the above approach #include<stdio.h> int main() { int i, n=2; char str[50]; //open file sample.txt in write mode FILE *fptr = fopen("sample.txt
1 min read
scanf() and fscanf() in C In C language, scanf() function is used to read formatted input from stdin. It returns the whole number of characters written in it otherwise, returns a negative value.Syntax:int scanf(const char *characters_set)Time Complexity: O(n)Auxiliary Space: O(n) where n is the length of input.Many of us kno
4 min read
C fread() Function The C fread() is a standard library function used to read the given amount of data from a file stream. Defined inside <stdio.h>, the fread() function reads the given number of elements of specific size from the file stream and stores it in the buffer memory. The total number of bytes read by f
4 min read
fseek() vs rewind() in C In C, the functions fseek() and rewind() helps in manipulating the position of the pointer in the opened file but serve different purposes and have distinct working. The below table lists the major differences between fseek() and rewind() in C:Featurefseek()rewind()FunctionalityMoves the file pointe
2 min read
What is return type of getchar(), fgetc() and getc() ? In C, getchar(), fgetc(), and getc() all are the functions used for reading characters from input buffer. This buffer is standard input buffer for getchar() and can be any specified file for getc() and fgetc(). In this article, we will learn about the return type of these functions and why it matter
3 min read
Read/Write Structure From/to a File in C For writing in the file, it is easy to write string or int to file using fprintf and putc, but you might have faced difficulty when writing contents of the struct. fwrite and fread make tasks easier when you want to write and read blocks of data.Writing Structure to a File using fwriteWe can use fwr
3 min read