Hello World Explained

You have now run your first program in C! That is a good accomplishment already. Now we will look at each line in the program so that we understand exactly how it worked

#include <stdio.h>

The command printf is not part of the C language, but is provided in a library. The command is part of the library called "standard input and output" or "stdio" for short. The command #include must be included to allow the program to use a function from the stdio library.

int main(int argc, char* argv[])

This line begins the function called main. A function is simply a list of commands that are given a name (in this case, the name main). Functions will be explained in detail later in the webpages. For now, it is enough to know that every C program will begin by running the commands in the main function. To create your own C programs, you can simply copy this function and then change the commands within it.

The braces { and } indicate the start and end of the function main. Everything between the two braces is part of the function.

    printf("Hello world!\n");

printf is a command that tells the computer to display text to the screen. It is a function and the information contained in the brackets is the argument to the funtion. In this case, printf is being told to display the text Hello World!. The \n is a control character that tells printf to move to the next line after displaying the text (it is like hitting enter on the keyboard).

    return 0;

The final command simply tells the program that the function is done and the program can return from executing it.

In the next sections, we will go back and review the basic elements of programming, so don't worry if everything about this example was not clear right now. For the meantime, you can use the simple template below to write your own test programs as each concept is explained.

#include <stdio.h>

int main(int argc, char* argv[])
{

    /* YOUR CODE GOES HERE */

    return 0;
}

We will now explain the rules of syntax you must follow in the C language.

next


 home                      table of contents                      about