Skip to content
Topos Uranos

Summary

This lesson closes the first unit of the course, devoted to the first steps in programming with C++. It reconstructs the chain of concepts that runs from the specified problem to the error classified by its moment, passing through the algorithm, the source code, compilation and linking, the executable and the program’s communication with the outside world; it separates what of all this holds in any language from what is specific to C++; it gathers the undefined behavior that the unit has shown and the course rule that follows from it, always compile with warnings and, during development, with sanitizers; it repeats in its exit assessment the skills of the entry assessment; and it ends with six challenges that combine the whole unit: change with the fewest coins, an aligned table without loops, a program with five defects of five kinds, an experiment on the output buffer, a date with a fixed format and the compiler explained to someone who knows only an interpreted language.

Learning objectives

  1. Travel unaided the path from the statement to the program run: specify, write the algorithm, trace it by hand, write it in C++, compile it with the course options, run it and check its result.
  2. Explain which tool acts at each step of that path (editor, preprocessor, compiler, linker, sanitizers) and what each one receives and delivers.
  3. Recognize the kind of an error (compile, link, runtime or logic error) by how it shows itself, and fix several errors in the order in which they are revealed.
  4. Explain with an example what undefined behavior is and why the course compiles with warnings and sanitizers.
  5. Separate, in what was learned in the unit, what holds in any language from what is specific to C++.

What the unit achieved

The unit set out to take someone who has never programmed from a problem stated in everyday language to a program that compiles, runs, reads data and writes results, knowing what each tool did along the way. It has achieved this relying only on what the introduction declared as taken for granted: school arithmetic, the habit of following written instructions and the handling of files and folders.

From the problem to the algorithm

The lesson From the problem to the algorithm established that a problem is fixed by its specification, a contract that promises the postcondition to every input that satisfies the precondition and nothing outside it. An algorithm is a finite, precise and general procedure, which is written in numbered steps, traced by hand with a trace table and composed of sequence, decision and repetition; each repetition comes with a variant, a natural quantity that decreases at each iteration and guarantees that it terminates.

Programs, compilers and C++

The lesson Programs, compilers and C++ showed that the text of a C++ program is not run as it is: the preprocessor, the compiler and the linker turn it into an executable. From this followed the distinction that runs through the unit: what depends only on the text is discovered when compiling or linking, and what depends on the data, only when running. The lesson compared the compiled model with the interpreted one and justified the course’s use of C++23, requested with -std=c++23.

Installing the environment

The lesson Installing the environment on Windows, macOS and Linux installed a compiler capable of C++23 and Visual Studio Code with its C++ extension. Its lesson of method is that the editor does not compile, but calls a compiler that the terminal must find, and that the exact text of a message distinguishes an installation failure from an invocation failure.

Compiling, running and configuring

The lesson Compiling, running and configuring fixed the cycle of editing, saving, compiling, running and reading, and the course command. It distinguished the error, which prevents the executable, from the warning, which allows it but points to suspicious code; it turned on the sanitizers (the memory error sanitizer, AddressSanitizer, and the undefined behavior sanitizer, UndefinedBehaviorSanitizer), which watch over the run, and showed their limits; and it explained that file, compiler and console must agree on UTF-8.

Anatomy of a program

The lesson Anatomy of a program dissected the minimal program: the headers, which declare each dependency; main, the entry point whose value is the exit code; the namespace std; statements, blocks and comments. It taught how to read messages starting from the first one and fixed the course style guide.

Input, output and errors

The lesson Input, output and errors put the program in contact with the outside world: std::println writes exactly what is asked, with format specifications, and std::cin reads and stops at the first character that does not fit. A read that runs into a character that cannot form the number sets the variable to zero and leaves the stream in error, silently (a number that does not fit in the type or an exhausted input leave another value in the variable, and the lesson lists those cases); that is why the read is checked. The lesson closed the unit by classifying errors by their moment: compile, link, runtime and logic errors.