< Introduction  |  Index  |  Lesson 2: Simple Window>


Lesson 1:  Hello World


No doubt, if you have been looking into other programming tutorial texts before this one, you have come across the “Hello World” program. In case you have not, the code is provided below. Please open your favorite text editor and type in the program.


#include <stdio. h>

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

    return 0;
}

Save hello.c


Once you have typed this program into your text editor, save it as "hello.c" in a convenient location on your hard drive. This file is commonly referred to as a "source file". In C, source files generally end with the ".c" extension.

Compile and Run hello. c Select the shell and cd to the directory where you saved hello.c.

Type the command gcc hello.c.

GCC Error The compiler will take a short while to load and process the code. Assuming you have typed the program exactly as presented above, the compiler will remain silent while it works. If you do receive an error or warning message, go back to the text editor and double check that you entered every character in the file correctly and try again.

After the compiler completes its task, the command prompt will return. Issue the ls or dir command to obtain a directory listing. You will notice that the directory contains a new file named a.out. a.out is the default name that GCC gives to the executable files that it creates. To run the program type a.out and hit enter. The text "Hello World!" should appear (without the quotes) followed by the command prompt.

Congratulations! You've just compiled your first Amiga program!

Now, let's take a look at what is happening in this program before we move on to more interesting topics.

The first line reads #include <stdio.h>. This is called a preprocessor directive. All preprocessor directives begin with a "#" character. There are many of these directives, but the most common is #include. The text following #include is always the name of a file and usually ends with the ".h" extension which means "header file". Header files contain more code that helps the compiler process your program. In this case we are including a file called stdio.h.  stdio.h is one of the most used files in an ANSI C compiler because it contains information necessary to print text to the shell. Without stdio.h our program would not print anything at all. The arrow brackets around stdio.h tell the compiler where to look for the file. If arrow brackets are used to surround the file name, the compiler will look in its "include directories" in search of this file. These include directories can be changed or added to, but for now the default include directories will be sufficient. We could have written #include "stdio.h" instead of using the arrow brackets. Using quotes around a file name tells the compiler that this file is either in the current directory or that the text is an absolute path to the file. Before actually compiling the program, the compiler opens every file that has been included and essentially appends or inserts their contents into your program. To the compiler these separate files all appear as a single source file.

The next line is written int main(void) and is called a function declaration. A function in C is similar to a mathematical function: both take input arguments, process the arguments, and return a result. For instance, the trigonometric function y = sin(x), which is commonly found on scientific calculators, takes an argument (x), processes it according to trigonometric principles, and returns the result in y. In similar fashion, this line defines a function in C. The function's name is main() and it doesn't take any arguments at all because the keyword void is between the parentheses. It returns an integer number when it is finished processing as denoted by the keyword int at the beginning of the line (int is short for integer).

The main() function is a special function in C. Every program must contain a main() function or else the compiler will report an error. main() is the starting point for every program. When you run a program this function is always the first thing the operating system executes.

The next line only contains one character: "{". This is called an open curly brace and it tells the compiler that the function we just described in the preceding line is going to be defined (it also implies some other things that we will learn more about soon, but for now just think of it as the start of the function). Defining a function tells the compiler what to do when processing the function. The compiler will work its way downward from the open curly brace until it finds the corresponding close curly brace: "}". Everything between the two curly braces is called the function body and will be executed every time the function is called. In our program there is are two lines between the open and close curly braces. The first is:

printf( "Hello World!\n" );

There are many things to note about this line. First of all, this line is the first proper statement in our program. We know this because it ends with a semicolon. Most statements in C end with a semicolon and if you forget the semicolon the compiler will report an error. The semicolon tells the compiler that the statement has ended, much like a period (".") does in the English language.

This statement is also a function call. We can tell this because it contains text (printf) followed by parentheses.  All function calls in C are of the form result = function_name( arguments ). In this case we have only one argument called a character string or just "string" for short. A string is a series of characters that begin and end with quotes. The quotes are not part of the string, but rather denote its beginning and end. All characters between the quotes are treated as a single entity.

The printf() function is used to print text to a shell and is short for "print formatted". The text it prints is passed as a string argument between the parentheses. printf() returns a non-zero value if there was an error printing the text, otherwise it returns 0. Since we are not terribly concerned about possible printing errors at this point we can ignore this return value. (Technically speaking C uses only functions. Functions, by definition, always return a result. C, however, allows some functions to return a result called void, which essentially means "return nothing". Functions which return void are commonly called subroutines. In C terminology all subroutines are functions, but not all functions are subroutines. )

At this point you may be asking yourself two questions. First, how does the compiler know what to do when the printf() function is called?  And why doesn't the program print the "\n" at the end of the string?

The answer to the first question is slightly involved, but it has to do with the #include <stdio.h> line at the top of the source file. This include file contains the definition of the printf() function. In addition to including the stdio.h file the compiler automatically finds the correct executable code in a separate file called a library. A library contains many pre-made functions that can be added to your programs, but you do not have access to their source code, only to their functionality. In this case the GCC compiler automatically "links" our program with the necessary library code to allow us to use the printf() function.

The answer to the second question is a bit easier to comprehend. There are some characters in a computer that cannot be printed on the screen, but none-the-less are required in a string. Examples of these are the tab and the new line characters. These characters cannot be "typed" into the string, but may very well be needed. To solve this problem the designer's of C allow a trick to be used to represent these special characters. This trick uses the backslash character as in indicator that a special character is required and the following letter is a code for the special character. In out case the sequence "\n" inserts a new line character (which is why the command prompt shows up on the next line when we run our program). Feel free to remove this "\n" from the string and recompile the program. When you run it after making this change you will notice that the output is run together with the command prompt.

The program ends with the return statement followed by a number. We will discuss this in more detail in the next lesson, but for now know that return exits the program.

Well, that pretty much sums up the "Hello World" program. Our next tutorial will show how to leave the confines of the command line and open an AmigaOS window and print some text inside.

< Introduction  |  Index  |  Lesson 2: Simple Window>

If you find any errors or have any questions or comments please e-mail me.

Last updated on Friday, November 25, 2005 0:24