.. www.engineerscur.blogspot.com: The reason why we write getch() at the end of the C program.

The reason why we write getch() at the end of the C program.

The reason why we write getch() at the end of the C program.

->Solution to hold program's output in turbo C and Dos-Box.


            We need to write getch() in the end of every program we write in C language.I am talking about the people who writes programs in the turbo C and Dos-Box .On the Linux it's totally different thing.Cause you are running the program in terminal and there you don't need to hold your output because totally opposite then turbo C or Dos-Box where while running your program without writing getch() at the end of the program output will be shown for the small time that our eye can't see glimpse of it.

                Okay solution is right here,let's take an example ,

#include<stdio.h>
#include<conio.h>

main()
{
         int number1,number2,answer;
          scanf("%d %d",&number1,&number2);
          answer=number1+number2;
          printf("Answer is: %d",answer);
}


                So when we run above program we will be asked for number1 and number2 cause we have used scanf() function.When we'll enter the number2 and hit enter the black screen will be go away and we'll not be able to see answer.So here's two solutions for that,

Solution 1-->

                     Right after running your program go to windows>user screen. And you will be able to see previous screen which will be displaying your answer.


Solution 2 -->

                      Rather than after running and going to user screen you will like to see the output during your program is running.

                      It is very easy to do so we have to make that black screen which is displaying our output to wait until we can see our output.We can do this by giving some input function at the end of the program so computer will display your output and ask you for the last input and it will not make that black screen disappear until you will provide some input.When you will provide some input there is no further instruction to do some operation so it will exit.

                   So your above code should look like this after modification. 

#include<stdio.h>
#include<conio.h>

main()
{
         int number1,number2,answer;
          scanf("%d %d",&number1,&number2);
          answer=number1+number2;
          printf("Answer is: %d",answer);
          getch();  
// or you can use scanf(); function as well but it will occupy an variable so it is not good idea to do so
}