What is a 'nice' standard way of holding the console open in C? I'm looking for something similar to cin.clear(), cin.get(); in C++.
- 29,421
- 31
- 118
- 202
-
Are you talking about Windows, perhaps ? – Paul R Oct 18 '11 at 20:03
-
When you say standard, do you mean portable? – ObscureRobot Oct 18 '11 at 20:06
-
1The notion of a "console" is not in any way *standard* in either C or C++, so you're starting with a false premise. – Paul R Oct 18 '11 at 20:06
-
1possible duplicate of [Why does my program's output flash and close in Windows?](http://stackoverflow.com/questions/1048975/why-does-my-programs-output-flash-and-close-in-windows) – pmr Oct 18 '11 at 20:09
-
@Paul R Oh, come one, do I really have to be so verbose in every single question I ask, even though my intent is clear? I obviously meant keeping the main function from returning and exiting until the user presses a key. – Paul Manta Oct 18 '11 at 20:09
-
2No verbosity issue here but lack of a good issue description and failure to google. – pmr Oct 18 '11 at 20:11
-
Your intent may be clear to *you*, but it's not at all obvious to me. "Holding the console open" is pretty vague. If you actually meant "keeping the main function from returning and exiting until the user presses a key" then you should probably have said that in your question. – Paul R Oct 18 '11 at 20:13
-
2No, it's really not obvious what "holding the console open" means. The C++ fragment helps, but not enough; you might do that for any number of reasons other than what you said. – zwol Oct 18 '11 at 20:14
5 Answers
puts("Press <enter> to quit:");
getchar();
That's assuming you need to do this in the program, which is probably not a good idea in general. And if I run your program from a shell, I'm going to be a bit annoyed at the extra step when I'm expecting the program to terminate nicely and let me have my next prompt.
- 242,098
- 41
- 402
- 602
-
1Thanks. I only use this in programs I'm asked to do in class, not in real world programs. – Paul Manta Oct 18 '11 at 20:12
I use the below 2 lines of code.
printf("Press ENTER key to Continue\n");
getchar();
- 234
- 3
- 8
I'm not sure but it should be getch(); however obscure robot's answer (if it's true) I need to his while struct.
how about:
while (1) { sleep 3600; }
Or do you need to be able to respond to a keypress? If so, and you want to stick to the standard C library, then use scanf. Beware of buffer overruns.
What are you trying to do here? If you just need to keep your terminal open when your program exits, that's something you should set in your terminal emulator, not in your program.
- 7,168
- 2
- 26
- 35
-
Yup, a key press would be nice. Besides, is there even a standard sleep function to being with? – Paul Manta Oct 18 '11 at 20:06
-
"that's something you should set in your terminal emulator, not in your program" -- This isn't for real-world programs. I'd only use it in small programs that I'm asked to do in class. – Paul Manta Oct 18 '11 at 20:07
-
@Paul: No, there is no `sleep` function in standard C (there is one in POSIX). – Keith Thompson Oct 18 '11 at 20:08
-
You can usually find sleep in unistd.h on unix boxes. Windows also has a sleep that is roughly compatible, but it lives in Winbase.h. – ObscureRobot Oct 18 '11 at 20:10
-