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:
#include<stdio.h>
#include<conio.h>
void main ()
{
clrscr();
char m;
printf("Enter character:");
m=getch();
printf("The character you Entered=%c",m );
getch();
}
Output:
Enter character:
The character u Entered=a
Syntax:
- For value input: Variable name= getch();
- For screen holding at the end of program: getch();
#include<stdio.h>
#include<conio.h>
void main ()
{
clrscr();
char m;
printf("Enter character:");
m=getch();
printf("The character you Entered=%c",m );
getch();
}
Output:
Enter character:
The 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
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.
Syntax:
gets(variable name);
Example:
#include<stdio.h>
#include<conio.h>
void main ()
{
char str[];
clrscr();
printf("Enter a string:");
gets(str);
getch();
}
Output:
Enter a string:hello
Syntax:
gets(variable name);
Example:
#include<stdio.h>
#include<conio.h>
void main ()
{
char str[];
clrscr();
printf("Enter a string:");
gets(str);
getch();
}
Output:
Enter a string:hello