Advertisement
Unformatted Console Input Functions | Description |
---|---|
getch() | Reads a single character entered by the user at the console, without echoing it. |
getche() | Reads a single character entered by the user at the console, and echoing it. |
getchar() | Reads a single character entered by the user at the console, and echoing it, but needs the Enter key to be pressed by the user at the end. |
gets() | Reads a single string entered by the user at the console. |
/* C - Example of getch(), getche(), getchar() */
#include<stdio.h>
#include<conio.h>
int main()
{
int ch;
printf("Running getch(), please enter a character : ");
/* getch() does not echo the pressed character key on the screen */
ch = getch();
printf("\n");
printf("The character value you have just entered : %c", ch);
printf("\n");
printf("Running getche(), please enter a character : ");
/*getche() function does echo the pressed character key on the screen */
ch = getche();
printf("\n");
printf("The character value you have just entered : %c", ch);
printf("\n");
printf("Running getchar(), please enter a character : ");
/*getchar() function echoes the pressed character key and also requires the ENTER key at the end */
ch = getchar();
printf("The character value you have just entered : %c", ch);
return 0;
}
Running getch(), enter a character :
The character value you have just entered : a
Running getche(), enter a character : g
The character value you have just entered : g
Running a getchar(), enter a character : w
The character value you have just entered : w
Advertisement
/* C - Using gets() function to read a string */
#include<stdio.h>
int main()
{
char country[20];
printf("Enter a country you want to visit : ");
gets(country);
printf("The country you want to visit is : %s", country);
return 0;
}
Enter a country you want to visit : United Kingdom
The country you want to visit is : United Kingdom
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement