Skip to content
Theory · Your First Program · lesson 1 of 7

How a C++ program works

What you'll learn

What you'll learn:

  • explain how the text of a C++ program becomes an executable file;
  • tell a compiler error from a linker error;
  • read a program's exit code and know where it comes from;
  • write your own function that takes numbers and returns a result.
The manuscript hello.cpp passes the translator's desk and the binder's press, which sews it with a std::cout page into the book hello

A C++ program is text, but the processor runs only machine code. That is why a program file cannot be run right away: another program first translates it into machine code.

Picture a book written in Russian for a reader who knows only their own language. Before it reaches them, the manuscript passes through two craftsmen:

  1. The translator checks that the manuscript follows the rules of the language and translates it. If they find a mistake, they send the manuscript back with a note, and there is no translation.
  2. The bookbinder sews the translated pages together with ready-made pages from the library. The result is a book you can hand to the reader.

A program goes the same way: the reader is the processor, and the translator and the bookbinder are two programs. Here is a program that prints one line:

C++
#include <iostream>

int main() {
    std::cout << "Hello, world\n";  // Hello, world
}

The line #include <iostream> inserts a description of the standard library's input and output tools into the file. Among them is std::cout, the program's standard output. The prefix std:: means that the name comes from the standard library. The << operator sends text to the output, and \n at the end of the text starts a new line.

Where execution starts

A function is a named group of statements: you call it by its name, and it can return a result. It is like a recipe with a name: say "omelette", and the cook goes through every step of the recipe.

A program starts executing in the function main, and a program has exactly one. Its body is the statements inside the curly braces:

  • the statements run one by one, from top to bottom;
  • the statement with std::cout ends with a semicolon;
  • after the last statement of main, the program ends.

From text to program

Let's follow the program from text to launch:

  1. The file hello.cpp that holds the program's text is called the source file.
  2. The compiler is the translator. It checks that the text follows the rules of C++ and translates it into machine code. It writes that code to an intermediate object file, such as hello.o.
  3. The linker is the bookbinder. It joins the translated code with ready-made code from the standard library, including the code of std::cout.
  4. The result is an executable file—the one the operating system runs.

If there are several source files, the compiler translates each one separately, and the linker builds one program from them. Usually one command runs both steps, so you hardly ever see the intermediate files.

Errors before and after launch

If the text breaks the language rules, the compiler reports a compile error and names the line where it found the error. No executable file appears then, and there is nothing to run.

This is where the translator picture stops working. A human translator would notice a typo, but the compiler checks only the rules, not what the author meant. It lets the typo in "Helo, world\n" through, and the typo shows only at run time—in what the program prints.

Besides errors, the compiler issues warnings. With warnings the executable file still appears, but the compiler points at a place that looks like a mistake.

Common mistake

Here the semicolon after the statement with std::cout is missing:

C++
#include <iostream>

int main() {
    std::cout << "Hello, world\n"  // ✗
}
TEXT
error: expected ';' after expression

This is a compile error: no executable file appeared, so the program never ran.

Key points

  • The compiler checks the program's text against the rules of C++ and translates it into machine code.
  • The linker joins the translated code with the standard library's code into an executable file.
  • Execution starts in the function main, and its statements run from top to bottom.
  • After a compile error there is no executable file and nothing to run.