17

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
user3128376
  • 974
  • 6
  • 17
  • 40
  • 2
    Run the .exe in command line, or add system("pause") at end of program. This also happen if you simply click the exe. Because windows close the command line immediately after program exit, just add a command that will pause. – moeCake Dec 23 '13 at 03:59
  • Your question title is Shows C++ and tag is on C... –  Dec 23 '13 at 04:16
  • 1
    @NishithJainMR: Despite the name, Microsoft Visual C++ _does_ include a C compiler. It's severely outdated (19 years) but that's still sufficient for such a simple program. – MSalters Dec 23 '13 at 08:04
  • Possible duplicate of [Preventing console window from closing on Visual Studio C/C++ Console application](http://stackoverflow.com/questions/1775865/preventing-console-window-from-closing-on-visual-studio-c-c-console-applicatio) – cxw Jul 20 '16 at 22:57
  • Use `for (;;)` or `while (1){;}` at the end of code. – EsmaeelE Nov 08 '17 at 23:46

10 Answers10

29

You can run the application in debug mode and in release mode. Normally Ctrl + F5 will run the application without debugger. And F5just 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 . . . 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...

11

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.

jpw
  • 44,361
  • 6
  • 66
  • 86
3

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.
Fiddling Bits
  • 8,712
  • 3
  • 28
  • 46
3
#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 ...

james
  • 41
  • 1
2

add this code before return 0 ;

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

or

getchar();
2

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.

MSalters
  • 173,980
  • 10
  • 155
  • 350
1

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();

}
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Debendra Dash
  • 5,334
  • 46
  • 38
0

I use Visual Studio 2013 for Python and I also struggle with that problem. My solution is to press F5instead 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.

0

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

gorilon
  • 424
  • 3
  • 12
0

You can also hold CTRL + F5 to get the window to stay open.