3

see i have compile one c program and prepare one a.exe

Now when ever i click on a.exe

cmd windows opens

a.exe run

and automatically close that windows.

What should i do in program or anywhere so this way after running that a.exe that cmd windows don't close automatically.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Jeegar Patel
  • 26,264
  • 51
  • 149
  • 222
  • 2
    This is definitely a duplicate. Hang on while I find it. – Mysticial Nov 26 '11 at 07:29
  • 2
    This is a duplicate of a duplicate of a duplicate of a duplicate of a duplicate of a duplicate of a duplicate of a duplicate of a duplicate of a duplicate of a duplicate of a duplicate of a duplicate of a duplicate of a duplicate of a duplicate. – Seth Carnegie Nov 26 '11 at 07:30
  • @Mysticial and Seth Carnegie If in that duplicate is ther any other solutin rather then using getchar() or makibg .batch file?? then please let me know and vote to close my question – Jeegar Patel Nov 26 '11 at 07:33
  • 1
    @Mr.32 there are 4 ways that I know of: `cin.get()` or equivalent, `system("PAUSE");`, running from the command line, or the batch file. – Seth Carnegie Nov 26 '11 at 07:39
  • 2
    Here we go: http://stackoverflow.com/questions/5148248/preventing-an-exe-file-from-closing: The first comment has a full list of them. I'm disturbingly surprised at how long it took to find this one. So I can't the OP for not being able to find it before asking. – Mysticial Nov 26 '11 at 07:41

4 Answers4

4

Create a batch file with the following content:

a.exe
pause

Then start the batch file

As an alternative you could add a line to your C program that prompts the user for input before exiting.

2

You could use getchar() which will read a single character from stdin:

#include <stdio.h>

int main(int argc,char** argv) {
  // getchar() waits until its able to read a character from stdin
  getchar();
  return 0;
}
Cyclonecode
  • 29,115
  • 11
  • 72
  • 93
2

Use system("PAUSE"); just before the return in your main().

mmtauqir
  • 8,499
  • 9
  • 34
  • 42
1

You can run your program from the command prompt:

Press winkey + R to open up the run prompt, and type in cmd. Then press enter. Now navigate to the directory where your program is by using the cd command, and run it from there.

Alternatively, you can add this to the end of your program:

#include <stdio.h>

int main() {
     // ...    
     getchar();
     return 0;    
}
David Hu
  • 3,076
  • 24
  • 26