“Hello World!”: A first example

Almost every programming course starts with the classic “Hello World” example. Go ahead and start your text editor and type the following code into it:

#include <stdio.h>

int main(int argc, char **argv)
{
    /* Print a message to the screen */
    printf("Hello World!");

    return 0;
}

Save this as hello.c in some easy-to-find pace. In Windows, the default working folder for MinGW is C:\MinGW\msys\1.0\home\username.

Open up a terminal (“Terminal” in MacOS or “MinGW Shell” in Windows). Use the cd command to go to the location that you placed hello.c. Once you are in the same directory as hello.c, run (without the “$”):

$ gcc -o hello hello.c

If you typed everything correctly, that should just return and give you another prompt. If you get an error, go back and make sure you typed everything correctly.

Once this command has run correctly, the current folder should have a filed called hello or hello.exe on Windows. Now we can run our program:

$ ./hello
Hello World!

Congratulations! You just wrote and compiled your first C program. Now, let’s examine how it works one line at a time:

#include <stdio.h>

This line tells the compiler to include the “stdio.h” header file. Header files define additional functions that a program can use. The “stdio.h” file defines functions for input and output. Specifically, it defines the “printf” function that we use below. We will talk much more about header files and their specifics in Unit 2.

int main(int argc, char **argv)

This line defines a function called “main”. Every C or C++ program has a “main” function that acts as an entry point for the program. When you run your program the operating system loads it from the hard drive into memory and then runs the code inside the “main” function. This is different from other languages such as Python or MATLAB where you can simply start writing code at the top of the file.

We will discuss functions more later on. For now, simply know that your program needs one of these.

{

An open brace starts what we call a block. The block contains all of the code between the “{” and the “}”. Blocks are used for grouping lines of code together; there are many different kinds of blocks and blocks can contain other blocks. If you have a block within a block, then a “}” denotes the end of the inner-most block and the block containing it continues just like you would expect from parentheses or quotation marks in English. This particular block denotes the code associated with the function “main”.

/* Print a message to the screen */

This line is a comment. Comments do nothing whatsoever to the compiled code. They exist to allow the programmer to leave notes to themselves or whoever else might be reading their code. In C, comments are started with “/” and ended with ”/”. Comments in this style can even span multiple lines. While we probably didn’t need a comment here, putting comments in your code will make it much easier to read later.

printf("Hello World!");

This calls the “printf” function and prints “Hello World!” to the screen. There are a number of important things going on here. First, notice that “Hello World!” is in quotes. This tells the compiler that “Hello World!” is a string which is just a piece of text and not a command that it should try to interpret.

Second, notice the parentheses that surround “Hello World!”. This is because “printf” is a function. A function is self-contained piece of code that you call to perform some action. The action performed by the “printf” function is to write formatted data to the screen. In C function are called are always something of the form: “name(arguments)”. In this case, we are calling the function named “printf” with just one argument: the string “Hello World!”. When a function takes more than one argument, the arguments are separated by commas.

Finally, notice the semicolon at the end. That is because this line is a statement. A statement is something that gets executed. The function declaration two lines above is not a statement because it just tells the compiler about the function and its corresponding block. Every statement in C ends with a semicolon.

return 0;

The return statement causes the program to leave the current function regardless of where it is on the block. The “0” is the return value. For the case of the main function, a return value of 0 means that it succeeded, any other number means a failure. We will talk about return values more when we discuss functions.

So there you have it, your first C program.