C++ L02 – Analyzing hello world

On the previous post we talked about setting up a C++ development environment. You should be able to compile and run C++ programs.

On this post we will be analyzing the previous hello world program in order to become familiar with some of the language features and artifacts. Don’t worry if you don’t understand everything that is mentioned here. Most of the terms discussed, are going to be posts of their own.

Analyze World

#include <iostream>

using namespace std;

int main()
    cout << "Hello world!" << endl;
    return 0;

On the first line we have a pre-processor directive. Pre-processor directives start with ‘#’. The pre-processor is a program that runs before the compiler and it mostly performs text-editing tasks. With the #include directive we essentially copy and paste the contents of file iostream in place of the directive. iostream is a standard library header. We will discuss headers later on.

using namespace std;

Notice that the command ends with ‘;’. Every statement in C and C++ must end with ‘;’.

using is a keyword that declares a namespace that the compiler looks for identifiers (=names of things). If we didn’t include this statement, in order to use cout, we would have to preface it with it’s namespace (std) like this:

std::cout << "Hello World" << std::endl;

We also had to change endl to std::endl, because endl is also defined under the namespace std.

int main()

What we have here is a function definition. main is the name of the function and it is special. Most C/C++ program start by calling this main function. The definition of a function consists of it’s prototype (line 5) and it’s body. Function body is the sequence of statements between {} after the function prototype declaration (lines 6-9). When we run our program, every statement of main runs one after another.

 cout << "Hello world!" << endl;

cout is an object defined in the std namespace that represents the terminal output.

<< is an operator. Like mathematical operators, in C++ an operator symbolizes a calculation between some operands and it has a result.

The ‘calculation’ here is: feed the message “Hello world!” to the object cout. The result of the operation is the object cout. That way, we can chain the << operators and also feed endl, that is a line break, to cout.

Step 1:

 cout << "Hello world!" << endl;

Step 2:

 cout << endl;

On the terminal the characters “Hello world!” are printed, followed by the cursor moving to the next line.

You can feed more than “messages” to cout, like numbers.

On the last statement of main we have:

 return 0;

When a return statement is run, we exit the function that we return from. Since main is the entry point of our program, by exiting it we also exit our program.

The 0 is a “signal” to the operating system that the program did not face any fatal problem and it exited successfully. In case of error we should return a non-zero exit code.

Bonus Homework

Using the information above about cout, try to print a second line that reads:

5 + 5 = 10!

and a third line, using 5 times the “<<" operator:

Hello, yourname

Author: Lefteris Chatzipetrou

Lefteris is a Co-Founder and CTO over at HAM Systems. He has wide experience in electrical engineering including electronics and embedded systems, mobile, web services and video games development.

Leave a Reply

Your email address will not be published. Required fields are marked *