Skip to main content
Module 1 · Lesson 1 of 31/26 in the course~12 min
Module lessons (1/3)

Variables and data types

Warm-Up Stage

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).

C++
#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

TypeMeaningTypical size (64-bit)Idiomatic example
intWhole with sign4 bytesint retries {3};
doublePrev double floating point8 bytesdouble ratio {0.85};
charSingle ASCII character/byte1 bytechar grade {'A'};
boolLogical value (true/false)1 bytebool ready {true};
C++
#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"):

C++
#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:

C++
#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

Exercise#cpp.m1.l1.e1
Attempts: 0Loading…

Declare a variable called age of type int initialized to 30, then print it using std::cout.

Loading editor…
Show hint

Write int age = 30; (or int age {30};) and send age to std::cout << age;.

Solution available after 3 attempts

Exercise#cpp.m1.l1.e2
Attempts: 0Loading…

Declare a variable username of type std::string with value "Alice" and print it to std::cout.

Loading editor…
Show hint

Bid std::string username = "Alice"; and then std::cout << username;.

Solution available after 3 attempts

Exercise#cpp.m1.l1.e4
Attempts: 0Loading…

Declare a constant const int maxUsers = 128; and a Boolean variable auto isReady = true;, then print them with std::cout.

Loading editor…
Show hint

Combine const int maxUsers = 128; with auto isReady = true; before std::cout.

Solution available after 3 attempts

Check for understanding

Quiz#cpp.m1.l1.e3
Ready

What happens when compiling int whole {4.9}; using brace initialization in modern C++?

C++
int main() {
    int whole {4.9};
    return 0;
}
Answer options
Quiz#cpp.m1.l1.e5
Ready

What type does the compiler infer for the variable port declared with auto?

C++
auto port = 8080;
Answer options