The Hello World Program

After removing the comments from the Pelles C "Hello World" example program, this masterpiece which performs such an amazing service to humankind, looks as follows-na:

#include <stdio.h>

int main(void)
    printf("Hello, world!\n");
    return 0;

You can see easily which line is responsible for displaying the "Hello world!". This line uses the printf command. This function is not built into C directly, but has to be loaded in from a standard C library of functions, called "stdio.h".

This then explains the first line of the program, which tells the C compiler to load this particular library of functions. There are a number of other standard C libraries which you may use in your programs. We'll mention those in detail later. One reason for having C functions distributed over numerous C libraries is to prevent your program becoming bloated with a whole load of functions which your program doesn't actually need. In particular the C compiler itself can work faster. By specifying which function libraries you actually use, you prevent the C compiler searching through (or even worse, compiling) a single vast library every time you compile a program.

Note that the # and the angled brackets < and > are a required part of the syntax, and an error will result without them. The hash signifies that the particular line it is on gives a preprocessor command. In other words, it is something the C compiler must do before it can begin compiling your program proper. The angled brackets signify that the library to be loaded is a standard system one, and not one of your own making. You'll learn how to make your own libraries later.

The line which says return 0; is basically the line which tells the program to quit. The zero is simply a code sent back to DOS to tell it that your program had no problems when it ran. This explains the int of the line which says int main(void). This line tells the compiler that the main part of the program follows-na, between the curly braces { and }, and that the program will return an integer (whole number) to the operating system. The void in parentheses, tells the compiler that no information is given to the program by the operating system when it starts.

There are two final small things to notice. Every line of the actual program itself (between the curly braces) finishes with a semicolon. These are essential in C programs to delineate the individual C expressions. The reason for these is that you can put, if you want to, more than one expression on a single line. In fact the printf line and return line could be made into a single line. The way to tell the C compiler where one finishes and the next one starts, is with the semicolon. A major source of errors for beginners in the C language is missing semicolons.

The final thing to notice is the \n which appears in the printf command. This is a newline command. If you had an additional printf later in your program, whatever it printed would appear immediately after the "Hello world!". This might not be what you intend, and so you specifically tell C to put a new line in, ready for the next thing which is displayed on the screen. Of course sometimes, you actually intend things to follow on from one another, and you omit the \n, but C gives you full control over this aspect. Note also that everything which you want the printf command to display is placed between double quotation marks. The reason for this will become apparent later when we explain the other things which you can put inside a printf command.