Hello World!

Fire up those text editors! It's time to get coding! Let's start off really simple, displaying some text in the terminal:

#include <stdio.h>

int main()
{
    // Prints "Hello World!" to output
    printf("Hello World!\n");
    
    return 0;
}

Now to save (the text you saved is called source code), compile and run the program!

Compiling In GNU/Linux And OSX

Open your terminal emulator and navigate to the folder where your source code is saved. Then compile like this (assuming your source code is called hello.c):

cd /path/to/your/source/code/folder
gcc hello.c -o hello
./hello

The "-o" option tells the compiler to output a file called "hello".
"./" and the name of the executable runs the executable in the current working directory.

What compilation does is convert your source code into machine readable instructions (called object code) and then link them into an executable in the linking stage. Well that's the gist of it, if you want an in-depth discusson of compiling I'm sure you can find some websites about it by searching "compiling linking" or something. In fact calling "gcc" does both the compiling and linking stages. You can compile and link separately like so:

gcc -c hello.c
gcc hello.o -o hello

The "-c" option indicates to only compile into an object file. The file will be called the name of the source file with ".o" instead of ".c", so there is no need to call "-o". Linking is then achieved by calling gcc on these object file(s).

Compiling In Windows

Compiling in Windows is similar, run the command prompt:

cd c:\path\to\your\source\code\folder
gcc hello.c -o hello.exe
hello.exe

Explanation Of The Source Code

First, on the top of the source code, is the preprocessor directive: "#include". The angled brackets tells the compiler to look for header files in its include directory (in GNU/Linux it's in /usr/include). If you want to include local header files, then use double-quote marks, (#include "file.h" for example). These header files contain functions that you will need to compile the program (stdio stands for Standard Input Output). "printf()" is one of these functions, and it prints to the output.
"int main()" is also a function, and it is a special case in that it is always executed first when running the program. The word "int" before main tells us that it returns an integer upon completion. This is the purpose of the statement return 0, to tell us that the program completed successfully. You can check what the program returned by typing in the terminal after it has run:

echo $?

It should be 0! By the way, I'm not sure how to do this in Windows...

If you are getting confused about all this talk about functions and return values, fear not! We will discuss functions more in-depth later in this series of tutorials.

Next are the first of a pair of curly braces. Inside the curly braces are the statements that the main function contains. The line that starts with "//" is a comment, and is ignored by the compiler. This is useful for explaining your code to other people or for yourself. "//" only comments a single line, for a multi-line comment we will use:

/* A multi
line
comment */

Everything in between "/*" and "*/" (inclusive) is ignored by the compiler.

Then there is the printf function, which prints to the output everything that is in the double-quote marks. The "\n" is a newline character, like pressing "Return" on your keyboard. Pressing the return key in your source code won't make printf output a newline. Because of this, the statements can be split into separate lines if required. How does the compiler know when a statement ends and begins? Well you have to tell it with the ";" key. That finishes our printf function. After the return 0 statement we end this block of statements with the ending curly brace.