Why getch() is used for?
Most of the c program end with this statement. So most of the people thought that this program is used to get output on the screen. But it is wrong This statement is used to read a single character from console without an echo to screen.. let see how it is....
Through a program
#include<stdio.h>
main()
{
printf("Hello friend");
}
When you run this program using turbo c++ compiler we cannot see the output. Actually the out put was already printed on the screen , at the printing time itself the run window will close. When you go to the user screen (window --> user screen) , you can always see what the last status of program running. Here getch() hepls to the user for waiting to enter a character from keyboard ..
Just go through another program.
#include<stdio.h>
main()
{
char name[20];
printf("Hello friend, What is your name?\n");
scanf("%s",&name);
printf("Hello %s Welcome to c programming",name);
}
In this program also I not use getch() . When we run the program We can see like this
Hello friend, What is your name?
Athil
Here we cannot see the last statement while running. But the last statement is already printed on the screen. After printing the run window will close without any response(Entering any character) of the user. After running the program just just go to the user screen. there we can see the last statement.
Hello Athil Welcome to c programming.
I hope that most of them understand what the use of getch() . If not then go through the bellow program
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int a,b;
printf("Enter any two numbers\n");
scanf("%d%d",&a,&b);
printf("Please press any key to view out out\n");
getch();
clrscr();
printf("The sum of the numbers you entered is %d",a+b);
getch();
getch();
}