2

Possible Duplicate:
How to stop C++ console application from exiting immediately?

I created an exe file in c. When I run it command prompt opens and then closes quickly and I cannot see the output. The program takes no runtime values from users. It reads data from a file. Is there any way to prevent this?

Community
  • 1
  • 1
samir rai
  • 21
  • 1
  • 1
  • 6
  • 2
    This has been asked many times before: http://stackoverflow.com/questions/2725823/how-do-i-get-the-screen-to-pause http://stackoverflow.com/questions/2529617/how-to-stop-c-console-application-from-exiting-immediately http://stackoverflow.com/questions/902261/is-there-a-decent-wait-function-in-c , not to mention http://stackoverflow.com/questions/193469/how-to-make-visual-studio-pause-after-executing-a-console-app-in-debug-mode and http://stackoverflow.com/questions/900666/system-calls-in-c-and-their-roles-in-programming . – Adam Rosenfield Feb 28 '11 at 22:25
  • 1
    Oh, but those are C++ questions! – Johan Kotlinski Mar 01 '11 at 11:21

5 Answers5

12

Run it natively from the command line.

Let's say that your file is in C:\awesomeness.exe

Open the cmd, type cd C:\ and then type awesomeness.exe

Zirak
  • 38,920
  • 13
  • 81
  • 92
2

One classic way to do it:

#include <stdio.h>

int main() {
    puts("hai");
    getchar();
}

This will wait for keypress at the end.

Johan Kotlinski
  • 25,185
  • 9
  • 78
  • 101
0

Are you in Windows? If so, a quick and dirty way is to use the system() function in stdlib.h (make sure you include it) to execute the PAUSE command in the command prompt.

system("PAUSE");
gpcz
  • 806
  • 6
  • 8
0

If you are using Visual Studio, hit CTRL + F5 instead of F5 in order to run your program.

tibur
  • 11,531
  • 2
  • 37
  • 39
  • But in this way you lose the debugging facilities; a better approach, in an IDE, is to add a breakpoint just before the end. – Matteo Italia Feb 28 '11 at 23:31
  • Needlessly break? I suppose that works fine until user passes out the program as an exe and people run, whoops... what did it say? – Lee Louviere Jul 18 '11 at 20:32
-1

Using 'system("pause");' before ending main() is the most common method.

int main() {
    ...
    ...
    system("pause");
}
Caprooja
  • 855
  • 7
  • 11
  • `system("pause");` is the most common method [to avoid](http://www.gidnetwork.com/b-61.html). – Matteo Italia Feb 28 '11 at 23:30
  • by count of small assignment programs out there, it may in fact be the most common method, whether it is good or you like it or not. However, some feel a crusade is better than a simple correction. How about saying the following "system() is inefficient, x is better and does the same thing" It in fact gets to the point much quicker in almost the same amount of letters. – Lee Louviere Jul 18 '11 at 20:31