01 - program structure

This lesson is aimed at briefly explaining how a C++ program is constructed. A lot of mechanisms mentioned in this lesson will be explained in-depth in later chapters. You don't need to understand everything here. It's more of a framework to understand what you need to know.

Hello world

"Hello world" is a minimal program for the given programming language that can be successfully run and its result can be observed. This generally causes hello world programs to contain a single print statement and any other code necessary to start and finish the program.

By print I mean any form of observable output. Generally it's going to be text displayed in the shell in which the program is run.

1
2
3
4
5
6
7
#include <iostream> // include directive

int main() // function name + extra info
{ // function body begins here
	std::cout << "hello, world\n"; // the print statement
	return 0; // return statement
} // function body ends here

A C++ program can consist of multiple grammatical constructs so let's go one by one.

The first line is a preprocessor include directive. Generally, any line beginning with # will be for a special mechanism called preprocessor. Preprocessor isn't for writing instructions to execute, it's more of a mechanism to inform the compiler what is required to run the code that follows. In this case the code is informing the preprocessor to include input/output stream library - it will be needed to perform the print statement.

The first line also contains a comment. Comments are text which is ignored by the compiler. Throughout the tutorial, you will encounter lots of comments in the code.

The second line is empty. Whitespace (that is, invisible characters such as spaces, tabs, newlines) are ignored by the compiler (unless within quotes). They are only used to separate code so whether it's 1 or multiple whitespace characters doesn't matter.

Next is the function name plus some extra information (here: just the keyword int which specifies that the function returns an integer). This extra information shouldn't be a concern for now. The significant part is that function names are followed by () to indicate they are actually functions.

Next, there is the function body. Statements are put inside {} and by the convention, code inside {} is indented. The function body can consist of arbitrary number of statements (including even 0) and they are executed one by one, top to bottom. Each statement ends with ; which is analogical to . in human languages.

In the hello world program we have 2 statements:

  • The first one performs the print operation. Quoted text is sent to the standard character output. The syntax used here is a bit different but don't worry - it will be explained soon.

  • The second one specifies the result of the function (that is, what function returns). Throughout the tutorial, you will use a lot of functions and their results. In the special case of main function, its result is sent back to the operating system where the value 0 indicates successful execution.

Couldn't the program just contain the include directive and the print statement? Why a function is needed?

In some programming languages, such thing is actually possible but C++ places special rules on code outside any functions and for historical reasons the program begins execution at the main function. With programs consisting of multiple files containing the source code, this approach is also easier to reason about - there is no need to think what is the order of code between files. We only need to know that the execution starts in the main function, and then this function can call other functions, potentially written in different files.

Printing values

Standard character output (std::cout) accepts many kinds of data. Expressions of different types are separated by <<. \n is a special control character that denotes new line (AKA line break or line feed character).

1
2
3
4
5
6
7
8
9
#include <iostream>

int main()
{
	int x = 1;    // an object of type int (integer)
	char c = 'a'; // an object of type char (character)
	std::cout << "x = " << x << "\n";
	std::cout << "c = " << c << "\n";
}

Since the first expression in the next statement is also text, the 2 statements can be combined into 1:

1
2
3
4
5
6
7
8
#include <iostream>

int main()
{
	int x = 1;
	int c = 'a';
	std::cout << "x = " << x << "\nc = " << c << "\n";
}

Sometimes you might also see std::endl (end-line) used instead of new line character:

1
2
3
4
5
6
7
8
9
#include <iostream>

int main()
{
	int x = 1;
	int c = 'a';
	std::cout << "x = " << x << std::endl;
	std::cout << "c = " << c << std::endl;
}

The output of this code is the same, however std::endl is not just a new line character. It does something more, something which shouldn't really be done explicitly - it flushes the buffer. Outputting any data to the operating system is a task which requires some extra work (very often system-specific). Buffering the output, while not necessary is very often done for performance reasons. C++ standard streams accumulate data as it is passed in and only output it in bulk when needed. The reason for this is that regardless of data size, communication with operating system is an expensive (in computation and/or time) operation so it should be avoided when possible.

C++ standard streams automatically flush the buffer when necessary, so you shouldn't really be concerned with what is happening underneath. Using std::endl forces it to flush the buffer more than necessary, which generally only slows down the whole program. If you see examples online which use std::endl, you can simply rewrite them to use new line character.

For now, this should be enough to let you print values of different objects. In later lessons, you will learn about standard input (std::cin) and how to create first user-interactive programs that read text.

Watch out for these syntax problems:

  • double operator: std::cout << << "text";

  • unwanted semicolon: std::cout << "text"; << "text";

  • operator with no operand: std::cout << "text" <<;

  • wrong operator: std::cout >> "text";

Exercise

  • Edit the hello world program, e.g. change the printed message.

  • Introduce random syntax errors to the hello world program (e.g. missing ; or <<) and observe compiler errors.

  • Try removing include directive. Observe errors when program is built.

  • Try removing the unnecessary return statement.