Master C++

Your first program

3 / 235
Contents

Contents

Master C++

0 of 235 complete

11 min read

Hello, and what every line of it means

The five lines every C++ program begins with, and what each one is actually for.

The traditional first program prints a greeting and stops. It is traditional for a good reason: it is the smallest thing that proves the whole chain works, from the text you typed, through the compiler and the linker, to words on a screen.

In C++ it is five lines rather than one, and that is worth being honest about rather than skipping past. Four of those lines are scaffolding you will eventually write without thinking. Here is the whole thing, and then every line of it in turn.

#include <iostream>

int main() {
    std::cout << "Hello.\n";
    return 0;
}
The whole program. Nothing has been left out or abbreviated.

The include: #include <iostream>

C++ knows nothing about screens. Printing is not part of the language itself; it is provided by the standard library, the collection of ready made code that comes with every compiler. That library is divided into pieces called headers, and you ask for the ones you need by name.

#include <iostream> says: before compiling this file, paste in the contents of the header called iostream. The # marks it as an instruction about the text of your file rather than a step your program carries out. The name is short for input and output streams, and it is the header that describes the screen and the keyboard.

Where the program starts: int main()

A function is a named block of instructions. Functions get a section of their own later on; for now you need exactly one, because every C++ program must contain a function called main, and that is where the operating system starts your program. Not the top of the file. main.

The round brackets after the name are part of what makes it a function rather than something else. The int in front says that main hands back a whole number when it finishes, and int is the C++ word for a whole number. That number is how a program reports whether it worked, which is what the last line is about.

The curly brackets { and } fence off the function's body: the list of instructions inside it. Everything between them is carried out in order, from the top down, and then the function is finished.

The printing line: std::cout << "Hello.\n";

cout is the name of the standard output, which is where text goes when you want a person to read it. On this page it is the panel underneath the code. In a terminal window it is the terminal.

The std:: in front is not decoration and cannot be dropped. Every name the standard library provides lives in a namespace called std, which is a way of keeping the library's names from colliding with the names you invent. std::cout means: the cout that belongs to std. Writing cout on its own will not compile.

The << sends the thing on its right into the stream on its left, and it reads nicely as an arrow pointing at the output. The semicolon on the end marks the end of one instruction, in the way a full stop marks the end of a sentence. Nearly every line inside a function ends in one.

Try CodeC++

Change the message between the quotation marks and run it again. The quotation marks are the fence around the text, so they never appear in the output themselves.

Handing a number back: return 0;

return ends the function and hands a value back. Here the value goes to the operating system, and the convention is old and universal: 0 means the program finished successfully, and anything else means it did not. A script that runs your program checks that number to decide whether to carry on.

Nothing moves to a new line on its own

\n inside a piece of text is not a backslash followed by an n. It is one character, and it means start a new line here. Text sent to std::cout goes out exactly as given, with nothing added, so two messages with no \n between them arrive stuck together.

Try CodeC++

Run it, then add \n just before the closing quotation mark on each of the three lines and run it again.

Several pieces in one instruction

<< can be used more than once in the same instruction. Each one sends the next piece, left to right, so a line can be built out of parts without joining them together first. The parts do not all have to be text: a number can go straight into the stream.

Try CodeC++

Change 7 to 7 * 52 and run it again. The arithmetic is worked out first and only the answer is sent, so the sentence needs rewording too.

A number sent that way is a number, not text, and C++ divides whole numbers the way you would divide sweets between children: whatever does not go round is left over rather than split up.

Try CodeC++

Change both 7s to 9 and run it again. The remainder is thrown away whenever both sides are whole numbers, and kept as soon as one side has a decimal point on it.

Check yourself

What does std::cout << "3 + 4"; put on the screen?

Which one of the five lines could you delete and still have a program that compiles and prints Hello.?

return 0;. main is the one function allowed to leave its return out, and when it does, the compiler treats reaching the closing brace as return 0;. Every other line is load bearing: without the include there is no std::cout to use, without int main() there is nowhere for the program to start, and without the braces the function has no body.

All lessons in Master C++