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.
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.
int getc(FILE *stream);
Input: g (press enter key) Output: g
An Example Application : C program to compare two files and report mismatches
The difference between getc() and getchar() is getc() can read from any input stream, but getchar() reads from standard input. So getchar() is equivalent to getc(stdin).
Input: g(press enter key) Output: g
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 (Source: http://en.wikipedia.org/wiki/Conio.h)
Like above functions, it reads also a single character from keyboard. But it does not use any buffer, so the entered character is immediately returned without waiting for the enter key.
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'
Like getch(), this is also a non-standard function present in conio.h. It reads a single character from the keyboard and displays immediately on output screen without waiting for enter key.
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'
This article is contributed by Vankayala Karunakar. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- What is return type of getchar(), fgetc() and getc() ?
- EOF, getc() and feof() in C
- getch() function in C with Examples
- Difference and Similarities between PHP and C
- Difference Between malloc() and calloc() with Examples
- What is the difference between single quoted and double quoted declaration of char array?
- Difference between "int main()" and "int main(void)" in C/C++?
- Difference between ++*p, *p++ and *++p
- What’s difference between header files "stdio.h" and "stdlib.h" ?
- What is the difference between "char a" and "char a"?
- What is the difference between printf, sprintf and fprintf?
- What’s difference between “array” and “&array” for “int array” ?
- What's difference between char s and char *s in C?
- Difference between C structures and C++ structures
- Difference between Structure and Union in C
- Difference between strlen() and sizeof() for string in C
- Difference between const char *p, char * const p and const char * const p
- Difference between %d and %i format specifier in C language
- Difference between float and double in C/C++
- Difference between fork() and exec()