Summary
This lesson breaks the minimal C++ program down into its pieces and explains the role of each one. The include directive declares which parts of the library the program depends on, and each component requires its own header, even if another header sometimes brings it in “transitively”; the main function is the entry point, and its return value is the exit code that the system receives; the namespace std gathers the names of the library, and the course always writes them with their prefix; statements end in a semicolon, braces group statements into blocks, and comments explain what the code does not say by itself. The lesson ends with the style guide that the course follows in all its programs, and with the way to read other people’s code that uses std::cout and std::endl.
Learning objectives
- Identify in a program the include directive, the
mainfunction, the statements, the blocks and the comments, and explain the role of each one. - Explain what
#includedoes and why each component of the library requires its own header, even if the program compiles without it on a particular compiler. - Fix a program with structural errors (unclosed brace, missing semicolon, omitted header) from the compiler’s messages, read starting from the first one.
- Rewrite a program according to the course’s style guide and justify each change.
The minimal program
In the previous lesson a program was compiled and run without being examined. Now it is read line by line, because each line answers a different requirement: one of the language, another of the operating system, another of whoever reads the code. This is the program.
// First program: writes one line and ends.#include <print>int main(){ std::println("Hello, world");}Hello, worldThe first line is a comment: it begins with two slashes and runs to the end of the line, and the compiler discards it; it is written for whoever reads the program. The second is an include directive: it asks that, before compiling, the header <print> be incorporated, where the standard library declares std::println. The blank line has no effect at all; it separates the preparation of the program from its content. The fourth line opens the function main, the place where execution begins, and the brace on the fifth opens its block, which closes on the last line. Inside the block there is a single statement, ending in a semicolon: the call to std::println, which writes the text between quotation marks and then a line break.
It should be noted that the program contains no statement telling it to end. On reaching the final brace of main, execution ends, and the program tells the system that everything went well; how it does so will be seen further on. It follows that five pieces suffice for a complete program: comments, directives, the main function, blocks and statements. The following sections examine them in that order, except the comments, which are treated together with statements and blocks.
In the greeting program, the directive is line 2; the main function occupies lines 4 to 7; its block runs from the brace on line 5 to the one on line 7; the only statement is line 6, and the only comment, line 1. If the comment is deleted, the program compiles just the same and produces the same output; if any of the other pieces is deleted, it stops compiling or stops being a program.
Loading the content…
Completed
Sign in to save your progress