Hello, World

When learning programming or learning a new language, getting the first problem to work can provide a feeling of accomplishment and satisfaction and traditionally is the first problem assigned in a C programming class. It can seem like something pretty hard to do as you have to:

  1. Log into your computer,
  2. Use an editor to create a file that contains your code,
  3. Get the compiler to compile and link your code,
  4. Fix any errors from the compiler and recompile until there are no compiler errors,
  5. Run your program and make changes to your program if the expected results are incorrect.
Create your program and use a filename that ends with a .c extension. Examples of filenames that you could use are helloworld.c, program1.c and prog1.c  If your compiler requires something else, then you should consult the documentation for your compiler on its filename convention for .c files.

The program source code should look like this:

#include <stdio.h>

main()
{


  printf("Hello, World\n");
}

If you're using GCC, the command to compile and link the program will look like:

gcc helloworld.c

and if the compile and link work correctly, it will create a file named a.out (on OSX and Linux) or a.exe (on Windows) in the current directory. To run the program on OSX and Linux, type:

./a.out

To run the program on Windows, type:

a.exe

If you're using Microsoft Visual C++, the command to compile and link the program will look like:

cl helloworld.c

This assumes that you have the Microsoft Visual C++ environment set up. I have a batch (.bat) file that sets up the environment and I run this batch file before working with Visual C++. The command to setup the environment in the batch file is:

call c:\"program files"\"microsoft visual studio .net 2003"\common7\tools\vsvars32.bat

This command and the location will vary with the compiler that you use and where the software is installed.

The command will create an executable file named helloworld.exe. To run the program, type:

helloworld.exe

or just

helloworld

if you don't have any files with the names helloworld.bat and helloworld.com in the same directory.

Expected Outout

D:\mozilla>a.exe
Hello, World

Discussion

#include <stdio.h> adds information about the standard library in your program
main() indicates that this is the main routine which is called by the operating system when you run a program.
printf("Hello, World\n"); calls the printf routine to print the text in quotes. The \n generates a newline so that the cursor ends up at the beginning of the next line after printing the text.
{ } a block of code that comprises the main() routine.

Return to Homepage    Return to Programming Pages
Updated November 24, 2005. For comments and questions, send email to Vector.x64 @ gmail.com (without the spaces).