All these functions read the character from input and return an integer. The value of EOF is used for this purpose.
getc()
It reads a single character from the input and return an integer value. If it fails, it returns EOF.
Here is the syntax of getc() in C language,
int getc(FILE *stream);
Here is an example of getc() in C language,
Example
#include<stdio.h> int main () { char val; printf("Enter the character: \n"); val = getc(stdin); printf("Character entered: "); putc(val, stdout); return(0); }
Output
Enter the character: a Character entered: a
getchar()
The function getchar() reads the character from the standard input while getc() reads from the input stream. So, getchar() is equivalent to getc(stdin).
Here is the syntax of getchar() in C language,
int getchar(void);
Here is an example of getchar() in C language,
Example
#include <stdio.h> int main() { char val; val = getchar(); printf("Enter the character : \n"); printf("Entered character : %c", val); return 0; }
Output
Enter the character : n Entered character : n
getch()
The function getch() is a non-standard function. It is declared in “conio.h” header file. Mostly it is used by Turbo C. It is not a part of C standard library. It immediately returns the entered character without even waiting for the enter key.
Here is the syntax of getch() in C language,
int getch();
Here is an example of getch() in C language,
Example
#include <stdio.h> #include<conio.h> int main() { char val; val = getch(); printf("Enter the character : "); printf("Entered character : %c", val); return 0; }
Output
Enter the character : m Entered character : m
getche()
Like getch(), the getche() function is also a non-standard function and declared in “conio.h” header file. It reads a single character from the keyboard and returns it immediately without even waiting for enter key.
Here is the syntax of getche() in C language,
int getche(void);
Here is an example of getche() in C language,
Example
#include <stdio.h> #include<conio.h> int main() { char val; val = getche(); printf("Enter the character : "); printf("Entered character : %c", val); return 0; }
Output
Enter the character : s Entered character : s