Starting with version 15.8.0 of Visual Studio 2017, which was released August 14, 2018, you have a new option for controlling whether the console window closes automatically when your C and C++ console-based application terminates, and what information you see when that happens.
Before version 15.8.0, if you ran your console-based program from inside the Visual Studio IDE, there were two behaviors:
- If you ran your program using Debug | Start Without Debugging (or by pressing CTRL-F5), then when your program terminated, the console window would remain open, the message “Press any key to continue . . .” would be displayed (after all of the output from your program, if any), and the console would pause waiting for you to press a key. When you pressed a key, the console window would close. This behavior allows you to see the output of your program, without having the console window close automatically.
- If you ran your program using Debug | Start Debugging (or by pressing F5), then when your program terminated (and assuming there were no breakpoints or other conditions that would cause your program to enter the debugger), the console window would immediately close, and you wouldn’t have a chance to see the output of your program. This behavior mimics the behavior of your program if you were to run it outside of of the Visual Studio environment (e.g., by double-clicking on the executable file itself).
- In this situation, if you wanted to use debugging features, but didn’t want the console window to close automatically at the end of the program, you’d either have to set a breakpoint at each return statement or exit call that might terminate your program, or you’d need to add some code to your application to explicitly pause before terminating. The latter approach is not recommended, because even if the code is protected by conditional compilation, it just adds extraneous source code.
This is how C, C++, and C# console applications behave.
Somewhere between versions 15.1 and 15.4, a bug was introduced into Visual Studio 2017 which changed the behavior described in #1 above. When this bug was present, Debug | Start Without Debugging (or CTRL-F5) would not display the “press any key” message, and would immediately close the console window when the program terminated. This bug changed a behavior that developers (and students) had counted on for many years. The bug was fixed in version 15.6.7. But the discovery and subsequent discussions of this bug likely contributed to the introduction of a new C and C++ option in Visual Studio 2017, which first appeared in version 15.8.
The New Option
You can find the new option in Visual Studio 2017 version 15.8 in Tools | Options | Debugging | General | Automatically close the console when debugging stops. When the option is enabled (checked), the console window behaves the way it always has, as described in the Background section above.
But when the option is disabled (unchecked) in a C or C++ console application, you get the following console window behavior:
- With the new option unchecked, when you run your program using Debug | Start Without Debugging (or by pressing CTRL-F5), the program runs to completion, a message is displayed in the console indicating that the process “exited with code 0” (or whatever the return code from the program was), the message “Press any key to close this window . . .” is displayed, and the console window waits for the user to press a key. So, the text that’s printed after program termination has changed, but it does still wait for user input as expected.
- With the new option unchecked, when you run your program using Debug | Start Debugging (or by pressing F5), the program runs to completion, and instead of automatically closing the window, a message is displayed in the console indicating that the process “exited with code 0” (or whatever the return code from the program was), followed by a message saying “To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops”, followed by the message “Press any key to close this window . . .”, and the console window waits for the user to press a key. So, this is all new behavior for starting the program under the debugger.
The following appears in the release notes for version 15.8:
“When debugging, the console window now stays open by default when the program terminates execution (similar to running the program without the debugger). This behavior can be toggled back to closing the console automatically in Tools > Options > Debugging > General.”
This is referring to Tools | Options | Debugging | General | Automatically close the console when debugging stops, discussed above.
Note: Some laptop keyboards require holding down an Fn key combined with the F5 key to get the F5 or CTRL-F5 mentioned in this post.
If you haven’t updated Visual Studio 2017 recently, and you want to pick up this new feature (present since version 15.8.0), go to Help | Check for Updates in Visual Studio.
If you’re curious or confused about why Visual Studio 2017 has a major version number of 15, have a look at this blog post, which shows the mapping between Visual Studio products and version numbers.