Initialization, assignment, and constants
Choose safe initialization, understand const, and avoid accidental changes to program state.
Learning objectives
- Tell a declaration, initialization, and assignment apart.
- Use brace initialization to make conversions safer.
- Use
constwhen an object should not be modified. - Explain why uninitialized local variables are dangerous.
Prerequisites: understand variable declaration, initialization, assignment, and basic types. Review Variables and data types if needed.
Three different actions
A declaration introduces a name and its type. Initialization gives a newly created object its first value. Assignment changes the value of an object that already exists.
int score{0}; // declaration + initialization
score = 10; // assignment
// int score{4}; // error: score is already declared in this scope
Think of initialization as setting the starting state of a new object. Assignment is a later update. They are related, but not interchangeable.
Prefer direct brace initialization
int attempts{3};
double price{19.95};
char grade{'A'};
bool submitted{false};
// int count{2.8}; // compile-time error: narrowingBrace initialization rejects many narrowing conversions: conversions that can lose information, such as a fractional floating-point value becoming an integer. Parentheses can allow that conversion, so braces are a useful default for ordinary object initialization.
Constants with const
A const object cannot be modified through that name after initialization. Use it for values that should remain fixed during the object's lifetime.
const double tax_rate{0.18};
double bill{100.0};
double tax{bill * tax_rate};
// tax_rate = 0.20; // error: tax_rate is constNotice that bill can change, while tax_rate cannot. This makes the program's rules easier to understand and prevents accidental updates.
const correctness in function parameters
In this example, total is passed by value, so the function receives its own copy. const prevents the function from changing that local copy; it does not prevent the caller from changing the original object. Later, you will see how a const reference provides read-only access without copying.
void print_total(const double total) {
// total = 0; // not allowed
std::cout << total;
}
Uninitialized variables
A local variable such as int count; has no usable initial value. Reading it before assigning a value leads to undefined behavior for an ordinary uninitialized automatic int. Do not expect it to be zero. Write int count{}; when zero is the correct starting value.
Trace a small program
int points{5}; // points = 5
const int bonus{2}; // bonus = 2
points = points + bonus;
// right side: 5 + 2 = 7
// assignment: points becomes 7
// bonus remains 2
Common mistakes
- Using a variable before assigning it a value.
- Assuming
constmeans the value is known at compile time; it means the object cannot be modified through that name after initialization. - Using assignment syntax in a declaration when you intended brace initialization.
- Removing
constjust to make an unwanted modification compile instead of fixing the design.
Practice
- Declare an integer
yearinitialized to 2026, then change it to 2027. - Declare a constant
days_in_weekand try to change it. Explain the compiler error. - Try
int quantity{4.5};and explain why the compiler rejects it. - Find every variable in a small program and check that it is initialized before its first read.
Practice solutions
Show worked solutions
- Write
int year{2026};followed byyear = 2027;. The first statement creates and initializes the variable; the second changes its value. - Write
const int days_in_week{7};. An assignment such asdays_in_week = 8;is rejected because a const object cannot be modified through that name after initialization. int quantity{4.5};is rejected because list initialization forbids this narrowing conversion.int quantity(4.5);is permitted but truncates the fractional part, so it stores 4; that may not match the programmer's intent.- Read each variable's declaration and initialization, then trace control flow. Every path that reaches a read must first pass through a valid initialization; do not assume a variable was initialized merely because it has a declaration.
Check your understanding
Why does int n{3}; n = 8; work?
Answer: The first statement creates and initializes n; the second assigns a new value to the existing object.
What does const guarantee?
Answer: The object cannot be modified through that const-qualified name. It does not automatically make every object reachable from it deeply immutable.
Summary
Initialize objects when they are created, use assignment for later updates, and use const to express values that must not change. Brace initialization catches many accidental narrowing conversions.