Skip to content
Free public beta until December 4, 2026: every course is open, no card needed. After that, the first chapter of each course stays free and full access is a one-time $39 ($19 for up to 100 founding members chosen for helpful feedback). Your progress is kept. How the beta works

What a program is

~12 min

A computer's processor understands only one kind of thing: very simple instructions, written as numbers. "Add these two numbers." "Copy this value over there." "If this is zero, jump ahead." Nobody wants to write a game or a spreadsheet that way. So people write programs in a programming language instead. A tool then translates them.

Think of a recipe written in English and a cook who only reads Morse code. Your recipe is the source code: text a person can read and edit. The cook is the processor. Before the cook can do anything, someone has to translate the whole recipe into Morse code. That translator is the compiler.

C++ does the translation once, up front. You write the text, you run the compiler, and it hands you a finished program file. After that, you run the program itself. Neither the compiler nor your text file takes part.

The program is a copy made at one moment. If you change your text afterwards, the program doesn't change with it. You have to translate again to get a program that does the new thing.

So there are always two files to keep apart in your head. There's the text you wrote, like hello.cpp. And there's the program the compiler made from it, like hello.exe on Windows or just hello on a Mac or Linux machine.

A program is a sequence of instructions a computer can execute. A programming language is a notation with fixed rules for writing those instructions in a form people can read. The text a programmer writes in that language is called source code, and it is stored in ordinary text files. C++ source files usually end in .cpp.

A processor cannot execute source code directly. It executes machine code: binary-encoded instructions specific to its processor family (x86-64, ARM64). A compiler is a program that translates source code into machine code ahead of time, producing an executable file the operating system can load and run. C++ is a compiled language: the translation happens once, before the program runs. Running the executable involves neither the compiler nor the source file, though it may load shared library files at startup (Chapter 1 Β§2 meets one such case).

The executable is a snapshot of the source at the moment it was compiled. Editing and saving the source changes nothing in an existing executable. The change takes effect only after the next compile.

Written by you, or made by a tool?

Sort each card: is it source code a person writes, or the program a tool produces?

Loading interactive…

Correct! πŸŽ‰ Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

What this task checks

Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.

The compiler says

A model answer

Correct answer

Also accepted:

Why

Why

Files ending in .cpp and the readable int main() { ... } lines are source code. The .exe file, the extension-less program on macOS/Linux, and the 0s and 1s are what the compiler produces.

Reading your first C++ program

You don't need to understand every symbol yet. Read it the way you'd read a recipe in a language you half know. Look for the words that carry meaning.

#include <iostream>

int main() {
    int apples = 3;
    int oranges = 4;
    std::cout << apples + oranges << '\n';
}
7

Worked example

Reading the program line by line

  1. 1

    #include <iostream> asks for the part of C++ that knows how to print text. "iostream" is short for input/output stream.

  2. 2

    int main() { starts the program. Every C++ program begins running at main. The { opens the list of steps. The matching } at the end closes that list.

  3. 3

    int apples = 3; makes a box called apples that holds a whole number (int), and puts 3 in it. The ; ends the step, like a period ends a sentence.

  4. 4

    std::cout << apples + oranges << '\n'; works out 3 + 4 and sends the answer, 7, to the screen. std::cout is "the screen," << means "send this there," and '\n' means "start a new line."

Predict before you run

Same boxes, new arithmetic: * means multiply and - means subtract. Type exactly what the program prints. The answer was checked by a real C++ compiler.

Loading interactive…

Correct! πŸŽ‰ Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

What this task checks

Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.

The compiler says

A model answer

Correct answer

Also accepted:

Why

Why

apples * oranges is 3 Γ— 4, which is 12. oranges - apples is 4 βˆ’ 3, which is 1. Each line ends with '\n', so the two answers print on separate lines.

The apples program goes through four stages on its way to printing 7. Click each one to see which file or tool is involved.

From apples.cpp to 7 on the screen

Click each stage. Which stage makes a new file, and which stages never need the compiler again?

Loading interactive…

Worked example

Change the text, run the old program

  1. 1

    You compile the apples program above and run it. It prints 7.

  2. 2

    Now you open the source file, change int oranges = 4; to int oranges = 10;, and save. You run the program again without compiling. It still prints 7. The program was made from the old text, and saving the file changed only the text.

  3. 3

    You compile again, then run. Now it prints 13. The rule is always the same: change the source, compile, then run. In Chapter 1 you'll type those steps as commands, and an IDE's Build button does the compile step for you.

From idea to running program

Put the steps in the order they happen.

Loading interactive…

Correct! πŸŽ‰ Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

What this task checks

Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.

The compiler says

A model answer

Correct answer

Also accepted:

Why

Why

You write and save the source code first. The compiler translates it, and only then is there a program to start and read output from.

Where this goes next

Chapters 1 and 2

Chapter 1 Β§3 turns this section into real commands. You save hello.cpp, run the compiler g++ (or clang++, or cl) on it, get a program called hello, and run that. Chapter 1 Β§4 does the same from an IDE. Chapter 2 Β§1 then opens up the single "compile" step from this section. That step is really three tools running in a row: a preprocessor, a compiler and a linker. Every error message you'll see comes from one of those three.

Where this shows up

Everyday software

Your web browser, the games on a console, and the infotainment and driver-assistance systems in a modern car are mostly written in C++, then compiled. When a company ships an update, they are shipping a newly compiled executable, not the source code. That's why you can run an app without ever seeing the text it was written in.

Check your understanding (counts toward your progress)

Fill the blank with one symbol so the program prints 12.

Correct! πŸŽ‰ Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

What this task checks

Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.

The compiler says

A model answer

Why

boxes * eggs is 3 Γ— 4, which is 12. With + in the blank, the same program would print 7.

Correct answer

Also accepted:

Why

3 and 4 have to combine into 12. Adding them only gives 7.

Read the program and type exactly what it prints.

Correct! πŸŽ‰ Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

What this task checks

Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.

The compiler says

A model answer

Why

chairs * tables is 4 Γ— 2, which is 8. The program prints 8 and then ends the line with '\n'.

Correct answer

Also accepted:

Why

* means multiply in C++.

Why can't the processor just run hello.cpp directly?

Correct! πŸŽ‰ Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

Size isn't the issue. Even a one-line C++ file is text the processor can't read.

A text editor is where you type the text, but it doesn't make the processor understand C++. The compiler still translates it.

Nothing about compiling or running C++ needs the internet.

Why

A processor executes machine code, simple numeric instructions for its own chip family. A compiler translates C++ text into that form once, before the program runs.

Put these lines in order to make a complete program that prints Hi.

Correct! πŸŽ‰ Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

What this task checks

Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.

The compiler says

A model answer

Why

#include <iostream> makes printing available. Then main opens with {, holds the printing step, and closes with }.

Correct answer

Also accepted:

Why

The #include line comes before everything else, and main's steps sit between its { and }.

Practice: Source code, compiler, program (4 problems Β· doesn’t affect your progress)

Which tool turns C++ source code into a program the machine can run?

Correct! πŸŽ‰ Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

A browser displays web pages. It doesn't translate C++.

A text editor is where you type the source code, but it doesn't translate it.

A spreadsheet app works with numbers in a grid. It doesn't translate C++.

Why

A compiler translates source code into machine code and writes an executable.

You type code in one of these. Which one produces the program itself?

A compiler

What does this program print?

Correct! πŸŽ‰ Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

What this task checks

Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.

The compiler says

A model answer

Why

pages - read is 120 βˆ’ 45, which is 75.

Correct answer

Also accepted:

Why

- subtracts the value on its right from the value on its left.

75

What does the ; at the end of int apples = 3; do?

Correct! πŸŽ‰ Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

Comments start with // in C++. The ; isn't a comment.

Multiplication is *. The ; only marks where a step ends.

Printing needs std::cout. The ; just ends the step.

Why

Most C++ steps (statements) end with a semicolon, the way an English sentence ends with a period.

Think of the punctuation mark that closes an English sentence.

Ends that step, like a period ends a sentence

Put the steps in order.

Correct! πŸŽ‰ Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

What this task checks

Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.

The compiler says

A model answer

Why

A change to the source only reaches the program after you compile again. The old program doesn't update itself.

Correct answer

Also accepted:

Why

A program can only run after it has been built, and a change reaches it only through another build.

D β†’ C β†’ A β†’ B

Feeling good about this section?

Take the section quiz

Every answer gets a full explanation at the end.

Sign in to save highlights

Insert an equation

  • No matches.