Master C++

Your first program

1 / 235
Contents

Contents

Master C++

0 of 235 complete

6 min read

What a compiler actually does

Your C++ is translated in full, once, before any of it runs. Almost everything else follows from that.

Imagine handing a recipe, written in English, to a kitchen where nobody reads English. There are two ways this can go. Someone can stand at the counter with a dictionary, translating each line just before the cook carries it out. Or someone can take the whole recipe away, translate every line of it, hand back a finished sheet in the kitchen's own language, and only then let anyone start cooking.

Both get dinner made. They fail differently. With the dictionary at the counter, cooking starts at once, and a line nobody can translate is discovered halfway through, with the pan already hot. With the whole recipe translated first, nothing starts until every line has been understood, and a line that makes no sense is found before the hob is lit.

Source code and machine code

The processor, the chip that does the actual work inside a computer, understands nothing that looks like writing. Its instructions are numbers: add these two, put the result there, go back four steps. That is machine code, and essentially nobody writes it by hand.

What you write instead is source code: ordinary text, in a language designed to be read by people, saved in a file. A compiler is a program that reads source code and produces machine code from it. C++ is a compiled language, which means it takes the second route in the kitchen. Every line is translated before any line runs.

Translated as it runsTranslated in full, first
StartsStraight awayOnly once the whole file is translated
A mistyped name on line 40Found when line 40 is reached, if it ever isFound before line 1 runs
Speed while runningUsually slower, because work the compiler could have done once has to be done again every time the program runsUsually faster, because that work was done once, before the program started
Languages that work this wayPython, JavaScriptC++, C, Rust
The two arrangements, and what each one costs you.

That is worth seeing rather than being told. Here is a short program. Pressing run does two separate things: it compiles, and then, only if that succeeded, it runs whatever came out.

Try CodeC++

Change the message between the quotation marks and run it again. Every piece of punctuation in this program is taken apart a couple of lessons from now, so do not read anything into it yet.

Now the same shape of program with one character taken out. Before you run it, decide what you expect: the first message and then a complaint? Or something else?

Try CodeC++

Not even the first line appeared, because no program was ever built to print it. Put a semicolon on the end of the second std::cout line and run it again.

The broken program above printed nothing at all, not even First line. Why not?

Printing is something a program does, and no program was ever made. The compiler read the file, reached a line it could not make sense of, and stopped without producing anything. There was nothing for the machine to start, so nothing ran and nothing was printed. Translated line by line instead, First line. would have appeared before the trouble was reached.

All lessons in Master C++