9

I'm trying out Visual studio 2010 by compiling a C program. After it displays the solution in the "DOS" command window, the window is immediately closed. In Visual studio 2008, one gets the message press any key to continue and pressing a key closes the command prompt window. How do I set this behavior in 2010?

2
  • 1
    It is no different in VS2010, pressing Ctrl+F5 invokes the "press any key" prompt when the program finishes. Apr 14, 2010 at 18:35
  • 1
    Yes, I know this shortcut for "start without debugging" but in my case the console window gets dismissed immediately. This is strange. Apr 14, 2010 at 18:41

8 Answers 8

18

After a bit of googling, I found this solution that does not involve modifying your code. It contains a workaround that involves modifying your .vcxproj file.

To do this from within Microsoft Visual C++ 2010 Express (I'm assuming it is similar for Visual Studio 2010), open your project and navigate to the following menu:

Project->$YOURPROJECTNAME Properties...
    ->Configuration Properties
        ->Linker
            ->System->SubSystem

Then use the dropdown to select Console (/SUBSYSTEM:CONSOLE) and apply the change.

"Start without debugging" should do the right thing now.

2
  • Wonderful. This works very fine. And I gave you 1 vote up also. I'm using the Express edition. Jul 6, 2010 at 15:07
  • This works great for me thank you. Is there a way to automatically apply this solution to all projects? Sep 3, 2014 at 14:29
3

The reason this happens is because now in VS 2010 it is possible to create an empty, generic C++ project by default, without having to go through the wizard. This causes VS 2010 to not properly set the Console (/SUBSYSTEM:CONSOLE) flag and so VS2010 has no idea it is a console application for which it would send the usual "Press any key..." prompt.

This problem doesn't occur if you create a Console Application project type from the New Project menu.

But then you can set this flag yourself and many others, through Project/Settings, as the above post has answered correctly!

2

This is normal. The "DOS" console window is attached to your program and is supposed to exit when your program finishes. If you want it to stay open, you need to add code to your program to keep it open. All you have to do is add a print statement and then input statement to the end of your program.

0
2

If you want it to stay open, you need to add code to your program. Like it

#include <stdio.h>
#include <stdlib.h>

... main(...)
{
    your codes
         .
         .
         .

     system("pause");

}

if you add this code ,your black screen stay at open. " system("pause") " inside the stdlib.h header file.

1
  • Your answer would be a bit better if you explained how this was supposed to work. As things stand it's a mysterious incantation and the reader doesn't learn much. Oct 27, 2012 at 1:14
1

FWIW, the "Start Without Debugging" command works in VS2010 exactly the same way it does for me in previous Visual Studio versions (that is, for a console project the console remains open when the process terminates with a "Press any key to continue . . ." prompt).

So, exactly how are you getting VS2010 to run your program? If you're using the "Ctrl-F5" shortcut, can you verify that Ctrl-F5 is bound to Debug.StartWithoutDebugging in "Options/Environment/Keyboard"?

Also, can you try running your program using the menu ("Debug/Start Without Debugging")?

3
  • Thanks. First I compile the file, then build it and then use Debug > start without debugging. I have assigned the "Ctrl-F5" as you told me but I get the same behavior. I admit that it's strange as Visual studio 2005 works fine as expected. Apr 14, 2010 at 21:31
  • @yCalleecharan: Interesting - I'm unaware of a setting that controls this. Apr 14, 2010 at 21:42
  • Yes, it's hard to believe this strange behavior. Apr 15, 2010 at 6:57
1

Bring "Start without debug" to the Debug menu...

Tools >> Customize >> Commands(Tab) >> Menu bar(drop down list) >> Debug(Controls Option) >> Add Command(button) >> Debug(Categories List) >> Start without debug

Using "Start without debug" Will allow VS2010 to display the "Press any key to continue" phrase before exiting.

0

If you start your application without debugger ("Start without debugging"), the console window will remain open until you press a key.

3
  • hmmm...actually I did that. I know this also applies in Visual studio 2008 and this "command" is what I use to run my programs. So I was a bit surprised of this different behavior in 2010. Apr 14, 2010 at 18:29
  • I can confirm that using "start without debugging" in 2010 doesn't keep the console window open after the program exits. Apr 14, 2010 at 18:40
  • Yes, with Visual studio 2008 it behaves as expected. I've restarted my machine but I still get the same strange behavior with 2010. Apr 14, 2010 at 20:34
0

You can add this function and call it right before returning from main():

void keep_window_open()
{
    // clear buffer
    fflush(stdin);
    printf("Please enter a character to exit\n");
    char ch;
    ch = getchar();
    return;
}

I adapted it from a C++ version in a utility header file at Bjarne Stroustrup's site: http://www.stroustrup.com/Programming/std_lib_facilities.h

3
  • Thanks a lot. I had to move the line "char ch" just after the opening brace { after the function definition so as to avoid compilation errors. I think that the line "fflush(stdin)" is C++ code and doesn't work well in C. Apr 14, 2010 at 20:33
  • You're right, according to the c FAQ: c-faq.com/stdio/stdinflush.html fflush is only defined for output streams. The link off that FAQ entry discusses how to flush input: c-faq.com/stdio/stdinflush2.html
    – Darel
    Apr 14, 2010 at 21:17
  • 1 vote up. This piece of code has been a savior. But vitavim (see the post on top) has found a correct solution for this bug and I'm accepting vitavim answer. Jul 6, 2010 at 15:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.