Use of getch(),getche() and getchar() in C
Overview
Most of the program is ending with getch(), and so we think that getch() is used to display the output...but it is wrong.It is used to get a single character from the console. Just see the behaviors of various single character input functions in c. getchar(), getche() , getch()
Function : getchar()
- getchar() is used to get or read the input (i.e a single character) at runtime.
Library:
stdio.h (Header File)
Declaration:
int getchar(void);
Example Declaration:
char ch;
ch = getchar();
Return Value:
This function return the character read from the keyboard.
Example Program:
void main()
{
char ch;
ch = getchar();
printf("Input Char Is :%c",ch);
}
Program Explanation:
Here, declare the variable ch as char data type, and then get a value through getchar() library function and store it in the variable ch.And then, print the value of variable ch.
During the program execution, a single character gets or read through the getchar(). The given value is displayed on the screen and the compiler waits for another character to be typed. If you press the enter key/any other characters and then only the given character is printed through the printf function.
Function : getche()
- getche() is used to get a character from the console and echoes to the screen.
Library:
conio.h (Header File)
Declaration:
int getche(void);
Example Declaration:
char ch;
ch = getche();
Remarks:
getche() reads a single character from the keyboard and echoes it to the current text window, using direct video or BIOS.
Return Value:
This function return the character read from the keyboard.
Example Program:
void main()
{
char ch;
ch = getche();
printf("Input Char Is :%c",ch);
}
Program Explanation:
Here, declare the variable ch as char data type, and then get a value through getche() library function and store it in the variable ch.And then, print the value of variable ch.
During the program execution, a single character gets or read through the getche(). The given value is displayed on the screen and the compiler does not wait for another character to be typed. Then, afterward, the character is printed through the printf function.
Function : getch()
- getch() is used to get a character from the console but does not echo to the screen.
Library:
conio.h (Header File)
Declaration:
int getch(void);
Example Declaration:
char ch;
ch = getch(); (or ) getch();
Remarks:
getch() reads a single character directly from the keyboard, without echoing to the screen.
Return Value:
This function return the character read from the keyboard.
Example Program:
void main()
{
char ch;
ch = getch();
printf("Input Char Is :%c",ch);
}
Program Explanation:
Here, declare the variable ch as char data type, and then get a value through getch() library function and store it in the variable ch.And then, print the value of variable ch.
During the program execution, a single character gets or read through the getch(). The given value is not displayed on the screen and the compiler does not wait for another character to be typed.And then, the given character is printed through the printf function.
Data Input and Output Programs
- Single Character Input Function : getchar()
- Single Character Input Function : getche()
- Single Character Input Function : getch()
- Single Character Output Function : putch()
- Single Character Output Function : putchar()
- Get or Read String Using gets()
- Print or Display String Using puts()
- Data Output printf and putchar() Example Program In C
- Data Input and Output gets and puts Example Program In C
- Printf And Scanf() Example Program In C Programming
Read More Articles
- Use of getch(),getche() and getchar() in C
- Switch Case Statement Example Program In C Programming Language
- C Character Set
- Convert a Floating-point value to an Integer in C
- Data Input and Output gets and puts Example Program In C
- Special Operators In C
- Pointer Representation and Pointer Example Programs
- C Data Input and Data Output
- Simple While Loop Example Program In C Programming Language
- Data Output printf and putchar Example Program In C
- C Introduction
- C Operators
- Storage Classes In C
- C Pointers
- File Management
- C Identifiers
- Loop Control Statements
- Hello World - Simple C Program
- C Array
- Single Character Output Function : putchar()
- C Reserve Words
- C Specific Properties and Implementation
- If else Statement Example Program In C Programming Language
- If Statement Example Program In C Programming Language
- Confusing Array in C ( Array Representation and Initialization )