Differences between Difference between getc(), getchar(), getch() and getche() functions

JavaObject Oriented ProgrammingProgramming

All of these functions are used to get character from input and each function returns an integer signifying the status code as well.

Following are the important differences between getc(), getchar(), getch() and getche() functions.

getc()

getc() can read characters from any stream. Returns EOF on failure.

Syntax

int getc(FILE *stream);

getchar()

getchar() can read characters from standard input only.

Syntax

int getchar();

getch()

getch() can read characters from standard input but it does not use any buffer and returns immidately without waiting for enter key pressed.

Syntax

int getch();

getche()

getche() behaves similar to getch() as it can read characters from standard input and it does not use any buffer and returns immidately without waiting for enter key pressed. Only differnce is that it prints the character as well.

Syntax

int getch();

Example

#include <stdio.h>
#include <conio.h>
int main() {
   printf("%c", getc(stdin));
   printf("%c", getchar());
   printf("%c", getch());
   printf("%c", getche());
   return 0;
}

Output

A
B
C
D
EE
raja
Published on 26-Nov-2019 15:11:58
Advertisements