简体   繁体   中英

How to prevent output screen from disappear in Visual Studio 2013 C++ Compiler

I just downloaded Visual Studio 2013. When I compile C, it doesn't show me my output. The output screen will show up for a brief second and then disappears.

#include <stdio.h>

int main()
{
    printf("hi");
    return 0;
}

"The program '[5688] Project1.exe' has exited with code 0 (0x0)." I know my code works and run correctly except I just can't make the output screen stay on without exiting after a second.

You can run the application in debug mode and in release mode. Normally Ctrl + F5 will run the application without debugger. And F5 just runs the application.

If you do Ctrl+F5 ("Start without Debugging"), the console remains open at the end and asks you to Press any key to continue . . . Press any key to continue . . . here you can see the output.

If you are just using F5 then you are in a debug mode. At the end you add, getchar() function before retuen 0; so the console will wait until you press any key...

Another option in addition to what's already been mentioned is to go into the properties for the project and change the Subsystem in the System section in the Linker options to Console (/SUBSYSTEM:CONSOLE) . Then the console window will remain when you run the program using ctrl+f5 (Debug/Start without debugging).

MSDN reference for the subsystem option .

#include <stdlib.h>
#include <stdio.h>
int main()
{
  printf("hello world");
  system("pause"); //this pauses the program until you press any key 
  return 0;
}

the output will be:

hello world

press any key to continue ...

add this code before return 0 ;

int  num;
scanf ("%d",&num);

or

getchar();

There's several things you can do (I'm assuming you're using Windows):

  1. Compile and execute your program using the Visual Studio Command Prompt program.
  2. Add getchar(); before returning to the OS.
  3. Add system("pause"); before returning to the OS.

I just put a breakpoint (F9 key) on the return 0 statement. Works only in debugging mode, but that's precisely what you want. If you run the program directly from the command line, it already works as intended.

I use Visual Studio 2013 for Python and I also struggle with that problem. My solution is to press F5 instead of Ctrl + F5 , then I will have 2 pop-up windows (console and program output).

I close the console window and the other will be closed together.

I first used the metioned getchar() and breakpoints solutions but this is no good if you want the program to end (for example if you are using a memory leak detector). I got over this by redirecting the output to a file. You can do this by inserting >output.txt in the command line option under the debug section of project properties

To keep your screen from closing you can use getchar() as follow in Visual studio:

#include "stdafx.h"
#include <stdio.h>
#include <iostream>
using namespace std;


int main()
{
    cout << "Hello\n";
    getchar();

}

您也可以按住CTRL + F5键使窗口保持打开状态。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM