I have a noob question about C....

McSkluvinMcSkluvin Regular
edited November 2011 in Tech & Games
I made my first "hello world" program and successfully compiled it, and when I open the .exe it clearly says what it's suppose to, but it just closes as soon as it opens. Here's the script I used:
#include <stdio.h>
int main(void)
{
printf_s("Hello, World!\n");
}

So my question is, is there some kind of code I need to add to it to keep the program from closing, or is there another problem I'm not seeing? :confused:

Comments

  • SlartibartfastSlartibartfast Global Moderator -__-
    edited October 2011
    you have two ways of doing this:

    1) open up cmd and launch the program from within the command

    or

    2) add getchar(); to the end of your program. This will cause the program to hang until you type a character (press a key), so:
    #include <stdio.h>
    int main(void)
    {
    printf_s("Hello, World!\n");
    getchar();
    }
    

    The reason it does this is because it assumes you're in a persistent console.
  • McSkluvinMcSkluvin Regular
    edited November 2011
    I didn't even notice that reply until now, wish I would've seen it earlier, but thanks anyway. :D

    You could also use:
    system("Pause");
    

    right?
  • SlartibartfastSlartibartfast Global Moderator -__-
    edited November 2011
    yep but that's Windows only. I also wasn't sure if it was in C - thought only cpp.
Sign In or Register to comment.