All of these functions read a character from input and return an integer value. The integer is returned to accommodate a special value used to indicate failure. The value EOF is generally used for this purpose.
getc()
It 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.
Syntax
int getc(FILE *stream);
Example
C
#include <stdio.h>
int main()
{
printf ( "%c" , getc (stdin));
return (0);
}
|
Input: g (press enter key)
Output: g
An Example Application: C program to compare two files and report mismatches
getchar()
The difference between getc() and getchar() is getc() can read from any input stream, but getchar() reads a single input character from standard input. So getchar() is equivalent to getc(stdin).
Syntax
int getchar(void);
Example
C
#include <stdio.h>
int main()
{
printf ( "%c" , getchar ());
return 0;
}
|
Input: g(press enter key)
Output: g
getch()
Like the above functions, getch() also 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.
getch() is a nonstandard function and is present in <conio.h> header file which is mostly used by MS-DOS compilers like Turbo C. It is not part of the C standard library or ISO C, nor is it defined by POSIX.
Syntax
int getch();
Example
C
#include <conio.h>
#include <stdio.h>
int main()
{
printf ( "%c" , getch());
return 0;
}
|
Input: g (Without enter key)
Output: Program terminates immediately.
But when you use DOS shell in Turbo C,
it shows a single g, i.e., 'g'
getche()
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.
Syntax
int getche(void);
Example
C
#include <conio.h>
#include <stdio.h>
int main()
{
printf ( "%c" , getche());
return 0;
}
|
Input: g(without enter key as it is not buffered)
Output: Program terminates immediately.
But when you use DOS shell in Turbo C,
double g, i.e., 'gg'
Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
22 Jun, 2023
Like Article
Save Article