Introduction to C Programming - The "hello world" Program

Chapter chap1 section 3

You may be wondering about the need for all the peculiar symbols in the "hello world" program. They are all essential as can be demonstrated by leaving some of them out, the consequences of such errors are discussed in more detail later. First, however, let's take another look at the hello world program.

main()
{
	printf("hello, world\n");
}

This program, in fact, consists of a single piece or chunk of executable code known as a function . Later on we will see programs consisting of many functions. All C programs MUST include a function with the name main , execution of C programs always starts with the execution of the function main , if it is missing the program cannot run and most compilers will not be able to finish the translation process properly without a function called main .

It is important to note that the required function is called main not MAIN . In the C programming language the case of letters is always significant unlike many older programming languages.

In the simple hello world program main appears on line 1. This is not essential, program execution starts at main not at the first line of the source. However, it is conventional in C programs, to put main at, or near, the start of the program.

The round brackets, or parentheses as they are known in computer jargon, on line 1 are essential to tell the compiler that when we wrote main we were introducing or defining a function rather than something else. You might expect the compiler to work out this sort of thing for itself but it does make the compiler writer's task much easier if this extra clue is available to help in the translation of the program.

On lines 2 and 4 you will see curly brackets or braces. These serve to enclose the body of the function main . They must be present in matched pairs. It is nice, but not essential, to line them up in the fashion shown here.

Line 3 is the meat of this simple program. The word printf clearly has something to do with printing. Here, of course, it is actually causing something to be displayed to the screen, but the word print has been used in this context since the time before screens were generally used for computer output. The word printf was probably derived from print formatted but that doesn't matter very much, all you need to know is that this is the way to get something onto the screen.

In computer jargon printf is a library function. This means that the actual instructions for displaying on the screen are copied into your program from a library of already compiled useful functions. This process of putting together a program from a collection of already compiled bits and pieces is known as linking and is often done by a linker or loader program as distinct from a compiler. Most compilation systems, sensibly, hide this complication from the user but you will see references to linkage or linker errors from time to time. More advanced programmers can build their own private libraries of useful things.

Line 3 is a statement. A statement tells the computer to do something. In computer jargon we say that a statement is executed. This simple example consists of the use of a library function to do something. In the C programming language a simple use of a function (library or otherwise) is, technically, an expression, i.e. something that has a value. In computer jargon we say that an expression is evaluated. To convert an expression into a statement a semi-colon must follow the expression, this will be seen at the end of line 3. The actual value of the expression is of no interest in this case and no use is made of it.

A C function normally consists of a sequence of statements that are executed in the order in which they are written. Each statement must, of course, be correctly written before the sequence is acceptable to the compiler. The following version of the "hello world" program shows the use of multiple statements.

main()
{
	printf("hello, ");
	printf("world");
	printf("\n");
}
Note that each one of the three statements is a printf expression converted to a statement by a terminating semi-colon.

Officially the purpose of printf is to write things to the standard output or screen. The list of things to be printed out are enclosed within parentheses immediately after the word printf. They are known as the function parameters or arguments. We sometimes say that the parameters are passed to the function. Incidentally when talking about any function, a C programmer would refer to it, in writing, by its name with the parentheses. We shall adopt this convention from now on.

In the hello world program printf() has only got a single parameter, this is the sequence of characters

hello, world\n

This sequence is identified as a single parameter by enclosing it in double quotes. Such a sequence of characters enclosed in double quotes is known as a string. Strings are discussed in more detail in the next section


Writing strings