Home »
C programming language
Single Character Input and Output using getch(), getche(), getchar(), putchar() and putch()
C - getch() Function
This function is used to get (read) single character from standard input device (keyboard) without echoing i.e. it does not display the input character & it does not require [return] key after input. getch() is declared in conio.h header file.
/*Compatible for TurboC compiler */
#include <stdio.h>
#include <conio.h>
int main()
{
char ch;
printf("Enter a character :");
ch=getch();
printf("\nEntered character is : %c",ch);
return 0;
}
Output
Enter a character:
Entered character is: G
Here, input character is G, which is not display while giving input.
C - getche() Function
This function is used to get (read) single character from standard input device (keyboard) with echoing i.e. it displays the input character & it does not require [return] key after input. getche() is declared in conio.h header file.
/*Compatible for TurboC compiler */
#include <stdio.h>
#include <conio.h>
int main()
{
char ch;
printf("Enter a character :");
ch=getche();
printf("\nEntered character is : %c",ch);
return 0;
}
Output
Enter a character: G
Entered character is: G
Here, input character is G, which displays character while giving input and does not require [return] after pressing ‘G’.
C - getchar() Function
This function is used to get (read) single character from standard input device (keyboard) with echoing i.e. it displays the input character & require [return] key after input. getchar() is declared in stdio.h header file.
#include <stdio.h>
int main()
{
char ch;
printf("Enter a character :");
ch=getchar();
printf("\nEntered character is : %c",ch);
return 0;
}
Output
Enter a character: G
Entered character is: G
Here, input character is G, which displays character while giving input and after pressing [return] key, program’s execution will move to next statement.
C - putchar() and putch() Functions
These functions are used to put (print) a single character on standard output device (monitor).
#include <stdio.h>
int main()
{
char ch;
printf("Enter a character :");
ch=getchar();
printf("\nEntered character is :");
putchar(ch);
return 0;
}
Output
Enter a character: G
Entered character is: G