C beginner - Hello World program not doing anything on Windows from command prompt

10,628

Solution 1

When you issue the gcc command you are actually COMPILING the source code, not RUNNING the program. The Compilation process creates an executable file to that you can run.

In the example by previous poster:

gcc -o helloWorld helloWorld.c

The -o parameter is the [o]utput name of the executable file to be created, let say "helloworld.exe" (as you are using Windows). This should create a new file helloworld.exe, Now you can RUN that file.

You should read a little bit more on general C programming and compiling.

Solution 2

You can write comments with // or /* ... */

Also to compile a c program with gcc use this:

gcc -o helloWorld helloWorld.c

So this should work:

#include<stdio.h>
int main(void)
{  // This is a comment
  printf("Hello world!\n");
  return 0;
} 
Share:
10,628
Wolff
Author by

Wolff

Updated on June 05, 2022

Comments

  • Wolff
    Wolff 5 months

    I'm about to start learning C (I need to program something in C, to convert it into MIPS later).

    I'm on Windows 8, and downloaded MinGW and set the path variable to its directory, so that the gcc or g++ commands should work in command prompt.

    I copied the following Hello World example into a .c text file.

    #include<stdio.h>
    int main(void)
    { // This is a comment
      printf("Hello world!\n");
      return 0;
    } 
    

    However, although I'm in the correct directory as the file, on command prompt when I type "gcc helloWorld.c" it pauses for a couple of seconds, and then doesn't do anything. It just acts as if the program has finished and allows me to type something else in.

    This is likely to be a simple mistake - I don't really understand what I'm doing. But can anyone help me out?