Module lessons (1/3)
Variables and data types
From raw memory to static type
Give a name and an exact size in bytes to each piece of data before even starting the program.
Each variable in C++ represents a well-defined area of memory (Stack) accompanied by a type known to the compiler. Unlike dynamic languages, the type never changes at runtime: the compiler directly generates optimal machine instructions for integers, floats or strings and immediately reports any inconsistencies.
1. Declaration and Brace Initialization ({})
In C++, declaring a local variable without an initial value leaves whatever sequence of bits was previously there in that memory cell (garbage value): reading it before assigning it is Undefined Behavior (UB).
#include <iostream>
int main() {
int classic = 25; // Inizializzazione classica (=)
int score {100}; // Brace initialization (C++11): evita restringimenti impliciti!
double taxRate {0.22};
std::cout << "Classico: " << classic << ", score: " << score << "\n";
return 0;
}brace initialization {} blocks dangerous conversions (narrowing conversions): writing int x {3.9}; produces a compilation error instead of silently truncating 3.9 to 3.
2. The Fundamental Primitive Types
| Type | Meaning | Typical size (64-bit) | Idiomatic example |
|---|---|---|---|
int | Whole with sign | 4 bytes | int retries {3}; |
double | Prev double floating point | 8 bytes | double ratio {0.85}; |
char | Single ASCII character/byte | 1 byte | char grade {'A'}; |
bool | Logical value (true/false) | 1 byte | bool ready {true}; |
#include <iostream>
int main() {
int items = 4;
double unitPrice = 12.50;
char currency = '$';
bool inStock = true;
std::cout << std::boolalpha << items << "x " << currency << unitPrice << " -> " << inStock << "\n";
return 0;
}3. std::string and the Difference with char
A single char uses single quotes ('A'). For sequences of variable characters C++ provides std::string in the <string> header, with double quotes ("Alice"):
#include <iostream>
#include <string>
int main() {
std::string username {"Alice"};
std::string greeting = "Benvenuta, " + username;
std::cout << greeting << " (lunghezza: " << username.size() << ")\n";
return 0;
}4. Immutability (const) and Type Deduction (auto)
By applying const, you commit the compiler to preventing accidental reassignments. With auto, however, you ask the compiler to deduce the exact type from the initial value on the right:
#include <iostream>
#include <string>
int main() {
const double pi {3.14159};
auto maxRetries {5}; // dedotto come int
auto appTitle = std::string{"Engine"}; // dedotto come std::string
std::cout << appTitle << " v1 (" << maxRetries << ", pi=" << pi << ")\n";
return 0;
}Practical exercises
Declare a variable called age of type int initialized to 30, then print it using std::cout.
Show hint
Write int age = 30; (or int age {30};) and send age to std::cout << age;.
Solution available after 3 attempts
Declare a variable username of type std::string with value "Alice" and print it to std::cout.
Show hint
Bid std::string username = "Alice"; and then std::cout << username;.
Solution available after 3 attempts
Declare a constant const int maxUsers = 128; and a Boolean variable auto isReady = true;, then print them with std::cout.
Show hint
Combine const int maxUsers = 128; with auto isReady = true; before std::cout.
Solution available after 3 attempts
Check for understanding
What happens when compiling int whole {4.9}; using brace initialization in modern C++?
int main() {
int whole {4.9};
return 0;
}What type does the compiler infer for the variable port declared with auto?
auto port = 8080;