-2

I installed the GCC compiler to write some C code, but when I navigate to the directory, and use the command gcc -o helloworld helloworld.c It makes an executable on my desktop like normal, but when I run it, the executable closes immediately

I don't think that the code is the problem, but it's a possibility.

#include <stdio.h>

int main()
{
  printf("Hello world\n");
  return 0;
}
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Jordan M. Baron
  • 241
  • 1
  • 4
  • 15
  • 1
    What is the error??? – OldProgrammer Jul 23 '16 at 21:51
  • There is no error, I use the gcc -o command in command prompt and when I get to run the executable, it crashes as soon as I open it – Jordan M. Baron Jul 23 '16 at 21:53
  • What does "crashes" mean???? – OldProgrammer Jul 23 '16 at 21:54
  • There is no error message, I have helloworld.c on my desktop and I used the command in my question – Jordan M. Baron Jul 23 '16 at 21:56
  • What is the symptom of that crash? A system message, a hang, or shut down? Even the tiniest clue could help. *"Joe: A man's life in these parts often depends on a mere scrap of information."* – Weather Vane Jul 23 '16 at 21:56
  • 4
    @JordanM.Baron Try adding the simply line `getchar()`to your file, and see if the command prompt then waits for a keypress. I don't think it is actually crashing, rather exiting after quickly printing out the text. – iRove Jul 23 '16 at 21:56
  • 1
    How exactly do you run the executable? If you double-click on it from an explorer window, it will open a new window for the output, run and complete, and then the OS will close the window, perhaps before you see it. Try running it from a command prompt. – Keith Thompson Jul 23 '16 at 21:56
  • BTW, both the code and the compiler command look ok. (I suggest `int main(void)` rather than `int main()`, but that's a trivial issue that has nothing to do with what you're asking about.) – Keith Thompson Jul 23 '16 at 21:57
  • I don't understand why I am receiving downvotes, but ok. – Jordan M. Baron Jul 23 '16 at 22:01
  • 4
    Not me, but perhaps the DV is because you are presenting a properly working program as a "crash" without saying how that manifests. – Weather Vane Jul 23 '16 at 22:05
  • Possible duplicate of [How to stop C++ console application from exiting immediately?](http://stackoverflow.com/questions/2529617/how-to-stop-c-console-application-from-exiting-immediately) – Mateen Ulhaq Jul 23 '16 at 23:32

2 Answers2

5

The problem is that Windows has poor support for running non-GUI programs.

A common way to run a program under Windows is to double-click the executable from an explorer window. For a program like yours that just prints to standard output, this will open a new window for the program's output, the program will run and quickly finish, and Windows will immediately close the window, perhaps before you have a chance to see it.

A common workaround is to add something to the end of your program, such as a call to getchar(), to cause the program to wait for input.

Another solution is to run the program from a command prompt. Its output will then appear in the current window rather than in a temporary one, and you'll see the program's output followed by a new prompt. If you run it that way, and added getchar() is unnecessary, and will make the program wait for input before terminating.

The Windows OS emphasizes GUI programs rather than programs that use plain text input and output. C was developed in a different kind of environment (though of course implementations of C for Windows support graphical operations).

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
3

You have missed this line getchar() in your code.

#include <stdio.h>

int main()
{
  printf("Hello world\n");
  getchar();
  return 0;
}

Note: Though, this is not the fix as @Keith Thompson explains in the other answer. Instead, this is a way where you can force the program from exiting until it waits for a keypress before the console window exits.


Another way (without using getchar())

Open the Command Prompt (cmd.exe), and navigate to the program's directory and run your program from there. You'll find that the window doesn't disappears anymore, rather it stays open.

Community
  • 1
  • 1
Raktim Biswas
  • 4,011
  • 5
  • 27
  • 32
  • 1
    That worked. Thanks. – Jordan M. Baron Jul 23 '16 at 21:59
  • 3
    Using `` is unnecessary here...the code would be more portable without it. **Edit:** Much better now ;) – iRove Jul 23 '16 at 21:59
  • 1
    @iRove thanks for notifying, removed it :) – Raktim Biswas Jul 23 '16 at 22:00
  • 6
    I suggest that adding a call to `getchar()` is a workaround, not a fix. The real problem is that Windows has poor support for running command-line programs. Running the program from a command prompt is another solution. – Keith Thompson Jul 23 '16 at 22:01
  • 1
    If this is run from the command line, it will unnecessarily wait for input and the user may not know why. I only use getchar() for small quick and dirty programs that test something and for which I don't want to open a command line and navigate to the directory, etc.etc. – Rudy Velthuis Jul 23 '16 at 22:02
  • @KeithThompson fully agreed to what you've said :) – Raktim Biswas Jul 23 '16 at 22:05