Summary
An imperative program does not compute its result all at once: it builds it step by step, storing intermediate values and changing them. This lesson studies the place where they are stored, the variable, which is a memory cell with a name, a type and a value. It teaches how to declare and initialize it with braces, and why braces are the preferred form; how to change its value by assignment, whose sign = does not mean what it means in mathematics; how to follow the state of a program, that is, the value of all its variables at each instant, with a trace table; how to choose names that say what the variable stores; and how to avoid the most silent error of this stage: reading a variable that was never given a value, which in C++ is undefined behavior.
Learning objectives
- Declare and initialize variables with braces, and distinguish initialization from assignment in a given program.
- Trace by hand the state of a program of successive assignments with a trace table, and thereby predict its output.
- Choose names in English that say what each variable stores, according to the rules of the language and the course guide.
- Explain why reading an uninitialized variable is undefined behavior, recognize it in a program and prevent it by always initializing.
The variable as a cell
In the lesson From the problem to the algorithm the algorithms were written with letters such as and , which took one value and then another: “ takes the value ”. In mathematics, a letter designates a fixed value, though an unknown one; in an algorithm, by contrast, it designates a place whose content changes. A C++ program needs the same thing, and it obtains it from the computer’s memory.
Main memory is a long sequence of bytes, each with its ordinal number, its address. A variable is a piece of that memory to which the program gives a name and a type. It therefore has four attributes:
- a name, with which the program designates it (
count,price); - a type, which fixes what values it can store, how many bytes it occupies and what operations it admits:
intstores integers,doublenumbers with decimals; - a value, the one it stores at a given instant, which can change while the program runs;
- a place in memory, which the compiler and the system choose and which the program almost never needs to know.
The image worth retaining is that of a cell: the name written above, the value inside. The type is the size and the shape of the cell; an int cell does not accept 2.5, just as a mail slot does not accept a parcel. The figures of this lesson draw the state of a program in this way, and redraw it after each statement.
That the type fixes the size can be seen: the operator sizeof gives the number of bytes that a variable or a type occupies.
#include <print>int main(){ const int count{12}; const double price{4.5}; std::println("count takes up {} bytes", sizeof(count)); std::println("price takes up {} bytes", sizeof(price));}count takes up 4 bytesprice takes up 8 bytesAn int occupies 4 bytes here, and a double, 8. Those figures are not the same on all systems, and on them depends how many distinct values fit in each type; the lessons Integers and their representation and Floating-point numbers will study it. Here the essential suffices: the type of a variable is fixed when it is declared and never changes; what changes is its value.
Loading the content…
Completed
Sign in to save your progress