Hello World is probably the simplest program you can write in C++:

#include <cstdio>

int main() {
    std::printf("Hello, World!\n");
    return 0;
}

If you are in Linux, save this content in a hello-world.cpp file. Open up a terminal and navigate to a destination where you’ve saved the file. For example, if you’ve saved the file on your desktop, type cd ~/Desktop in your terminal, which is like navigating to this folder in your file-browser. Alternatively, some file managers have a context menu where you can click Open terminal here which opens up a terminal in the folder you’re in right away. If you now issue the ls command, you should see your file there. Then, you can compile and run your first program using these two commands:

g++ hello-world.cpp -o hello
./hello

If you are in Windows, use the build and run button in your IDE.

You should see Hello, World! printed into the console.

That doesn’t look too complicated, right? Let’s break it down. First, I should explain what this program does. This is a so-called terminal application. That means it only runs in a terminal and has no graphical interface (GUI). Making graphical applications is a bit more complicated, so for now, we will only focus on terminal applications.

Now, back to the code.

  • #include <cstdio> says that we want to include a header file called cstdio. Header files are normal files containing declaration of functions, classes, variables and much more. In order to call a function in C++, we need to let the compiler know that the function exists and by including the proper header, we’re doing just that.

  • int main() {} defines a main function. In a program, there can be only one main function defined because it’s the first function that gets executed when the program starts. The empty () parentheses mean that this function doesn’t take any arguments. The int means that this function returns an integer. The body of the function starts with curly brackets {}. The opening bracket denotes a start of the function body and the closing bracket denotes an end of the function.

  • In the main function body, we’re calling an std::printf() function. This function only takes one argument - the text we want to print out. As explained above, we need to include an appropriate header to use this function. This function is defined in the cstdio header, so we needed to include it. Eventually, you’ll remember what header you need to include for what functions, but whenever you are unsure, you should get used to looking it up at cpp-reference where you can find most of the C++ std library documentation. The \n character at the end of the text just denotes an end of line, also known as line feed. (You can try compiling the program without this character to see what it does)

  • return 0; means the function returns the number 0 which means success. A non-zero return value from main function usually represents an error.

Note, at the end of each line in the function body, there is a semicolon (;) punctuator. This character must be present after every statement.

In the next chapter, we will look at the very basics of the C++ language. We will go over variables, variable types and simple math operations.