2.1. helloworld in C

Here is the source code of a C program that prints "Hello World!" to the screen:

#include <stdio.h>

int main(void)
  printf("Hello World!\n");

  return 0;

The first line is a preprocessor directive telling the compiler to include the header file stdio.h. The angle brackets around the name of the file to be included tell the preprocessor to look in the default location for stdio.h. On a Linux system this default location is typically the directory /usr/include.

The program consists of a single function (main) which has no parameters and an integer return type, in this case it returns 0 to the operating system after printing "Hello World!" to the screen.

If you save the source code to a file called helloworld.c, then to get an executable (runnable) version of the program, you have to compile the source by typing one of the following commands.

Using gcc, an ANSI C compiler:

gcc helloworld.c -o helloworld

Using cc, the default C compiler:

cc helloworld.c -o helloworld

If there are no compilation errors you will end up with an executable file called helloworld. If you omitted -o helloworld the executable would be called a.out. To run your program you simply type helloworld at the Unix prompt:

% helloworld

Hello World!


People running a csh shell may need to type rehash first.