Possible Duplicate:
What is the Best Practice for Combating the Console Closing Issue?
How do you keep the console from closing after the program is done in C? When I try to search for it I find lots of stuff about C++ and other languages, but nothing for C. Also, even for C++ there doesn't seem to be a definitive answer.
So could someone please let me know what the simplest way (doesn't need to be super elegant) way to keep the console open after a C program is done running?
The previous answers are all assuming that you want to invoke the console app and then essentially leave it "running" and waiting for user input to terminate. If this is the correct assumption, then +1 to GMan's answer. However, if you are asking how to invoke this console app from either a shortcut, Start->Run or some other mechanism and leave the cmd window open, then you will need to invoke it via cmd.exe
itself with the /k
option like so:
cmd.exe /k "foo.exe"
This will start a cmd window, run your console app, and then leave the cmd window open. This will address @Thanatos above. He's correct in that you should let the console app close. Again, for me it's unclear what you're really asking for what the end goal should be.
If I made the wrong assumption(s), feel free to -1 me.
run the program from the command line, instead of executing it directly.
Ctrl+F5 in Visual C++.
Console applications are meant to be run from the console. If you do that, after running you'll be left with your console window, and can easily view the output of your program.
You can use something like getchar()
to force the application to wait for a key-press.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With