Use of getch(),getche() and getchar() in C

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 run time.

Library:


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 thevariablechas char data type, and then get a value throughgetchar()library function and store it in the variablech.And then,print the value of variablech.
During the program execution, a single character is get or read throughthegetchar(). The given value is displayed on the screen and the compiler wait for another character to be typed. If you press the enter key/any other characters and then only the given character is printed through theprintffunction.

Function : getche()

getche() is used to get a character from console, and echoes to the screen.

Library:



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 thevariablechas char data type, and then get a value throughgetche()library function and store it in the variablech.And then,print the value of variablech.
During the program execution, a single character is get or read throughthegetche(). The given value is displayed on the screen and the compiler does not wait for another character to be typed.Then,after wards the character is printed through theprintffunction.

Function : getch()

getch() is used to get a character from console but does not echo to the screen.

Library:



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 thevariablechas char data type, and then get a value throughgetch()library function and store it in the variablech.And then,print the value of variablech.
During the program execution, a single character is get or read throughthegetch(). 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 theprintffunction.

Top Pages