Tuesday 14 December 2010

getche(), getch(), getchar()

getch() Function
“The getch = get character. This function is used to get a single character input from the user” during execution of program. It also force to wait the output to stay on screen until any key pressed from keyboard.

Syntax:
     For value input: Variable name= getch();
     For screen holding at the end of program: getch();
Example:
#include<stdio.h>
#include<conio.h>
void main ()
{
clrscr();
char ch;
printf("Enter character:");
ch=getch();
printf("you Entered=%c",ch );
getch();
}

Output:
Enter character:
 u Entered=a
Getche Function
The getche() = get character echo. It is also used to get a single character from the keyboard during the program execution. When this function is executed, the character entered by the user is displayed on the screen.

Syntax: Variable name=getche();
Example
#include<stdio.h>
#include<conio.h>
void main ()
{
clrscr();
char p;
printf("enter any character:");
p=getche();
printf("the character u entered=%c",p );
getch();
}


Output
enter any character:x
the character u entered=x

This special function is used to input string values from the user during execution of the program. As the user press enter key when typing after the string. A null character (\0) is automatically entered at the end of the string.

void main()
{
char ch;
clrscr();
//ch = getche();
ch=getch();
printf("Input Char Is :%c",ch);
getch();
}
/*
Explanation:
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 is get 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,after wards the character is  printed  through the  printf function.


No comments:

Post a Comment