-4

Many people have this question "How do you keep the console from closing after the program is done in C" but my question is a bit different. When i run the .exe a window opens and closes really quickly but why does the window open in the first place?

I mean... I get that if I tried to print something, a console window would be necessary but even if I'm running the sample code below a console window will pop up and I don't know why.

int main() {

    int i;
    for (i=0; i<100; ++i) {
        ++i;
    }
}
Community
  • 1
  • 1
  • 3
    If you build the program with console subsystem, you get a console. If you build it with GUI subsystem, you don't get a console. How to build depends on your toolchain. – Cheers and hth. - Alf Jan 18 '16 at 17:41
  • 2
    Your application opens a console because you've created a console application. If that's not what you want, create a different kind of project. – Igor Tandetnik Jan 18 '16 at 17:41
  • _"but even if I'm running the sample code below a console window will pop up and I don't know why."_ Probably because you run that program from your IDE. If you want to stop at the last closing brace (`}`) put a breakpoint there. – πάντα ῥεῖ Jan 18 '16 at 17:49
  • This is not related to a programming language, but Windows. – too honest for this site Jan 18 '16 at 17:50
  • @Olaf How is that related to windows specifically?? – πάντα ῥεῖ Jan 18 '16 at 17:50
  • @πάνταῥεῖ: Because other systems do not behave like that. But mostly because there is nothing about that stated in neither the C nor the C++ standard. It would be much the same for Ada or Pascal programs. – too honest for this site Jan 18 '16 at 17:51
  • @πάνταῥεῖ: Because the console is created by the OS, triggered by a flag in the PE header, and not by the programming language or its support library. – Ben Voigt Jan 18 '16 at 17:52
  • @BenVoigt That's not approximately true. IDE's running under other OS behave much the same, but OK ... – πάντα ῥεῖ Jan 18 '16 at 17:52
  • @πάνταῥεῖ: IDEs in other OSes may accomplish much the same effect, but they do so in a completely different way. – Ben Voigt Jan 18 '16 at 17:53
  • @πάνταῥεῖ: Agreed, it might be a matter of the IDE, too, where to configure that "feature" (it's not a bug ...). Still it is not related to the PL. – too honest for this site Jan 18 '16 at 17:55

2 Answers2

0

You have most likely compiled your code as a console program (which is pretty common for C++). As such the console will open in order to run your program. The console is not only used for input and output, but is also the "shell" in which your program runs.

Some GUI C++ programs will not open a console, but some of them will still open a console window in the background, and the GUI will launch from that.

Long story short, it's not something you need to worry about; console is more than just input and output.

M

swinefish
  • 559
  • 6
  • 22
0

Because the program executes the code you have told it to execute but as this jobs are very very easy for nowadays CPUs it may just take miliseconds to do the jobs so it does the jobs quickly and terminates ( Reason that console window is popped out ) and console window is shown in order to show you the results of program execution. To tell the program to stop and get a character from user just use this :

#include <conio.h> /* In visual studio */
int main() {

    int i;
    for (i=0; i<100; ++i) {
        ++i;
    }
    getch();
    return 0; /* By the way don't forget this, It may not give error if you omit this line but its good to write this line because it tells the OS how the program did its job */
}

But if you dont want use consoles you should try Win32 Api for gui programming in C which uses WinMain instead of main in console programs.