Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you keep the console from closing after the program is done in C? [duplicate]

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?

like image 466
Adam Avatar asked Dec 08 '09 01:12

Adam


3 Answers

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.

like image 165
nithins Avatar answered Sep 28 '22 07:09

nithins


  • run the program from the command line, instead of executing it directly.

  • Ctrl+F5 in Visual C++.

like image 28
Khaled Alshaya Avatar answered Sep 28 '22 07:09

Khaled Alshaya


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.

like image 45
GManNickG Avatar answered Sep 28 '22 07:09

GManNickG