getchar(), getch() and getche() | Difference in C | Difference between getchar(), getch() and getche()

All three functions are unformatted input functions that takes only one character at once from the user. But nature of taking inputs by each is different.

getchar();

After you press a character it will show character to be input and then you have to press enter key to take input.

getch();

After you press a character neither it will show character to be input nor you have to press enter key to take input.

getche();

After you press a character it will show character to be input But you don’t have to press enter key to take input.



 Example Program: 

//Program to copy
/* Difference between getchar(), getch(), getche()
Coded by SG Tech and EngT   */
#include<stdio.h>
main()
{
int ch1,ch2,ch3;
printf("Enter the character for getchar: ");
ch1=getchar();
printf("Enter the character for getch: ");
ch2=getch();
printf("\nEnter the character for getche: ");
ch3=getche();
printf("\nThe inputed characters are:"
"\n ch1= %c \nch2= %c \n ch3= %c", ch1, ch2, ch3);
return 0;
}

Output:

Enter the character for getchar: a
Enter the character for getch:
Enter the character for getche: c
The inputed characters are:
 ch1= a
 ch2= b
 ch3= c

Post a Comment

Previous Post Next Post