01 / 21

Initialization vs.
Assignment

Master the fundamental difference between giving a variable its first value and changing it later: a concept that shapes how you write correct, efficient C++.

C++

What Is a Variable?

A variable is a named region of memory that stores a value. Think of it as a labeled box: the label is the variable name, and whatever sits inside the box is the current value.

score 42 name value memory address 0x7ffc...

What Is Initialization?

Initialization is the act of giving a variable its first value at the moment it is created. The variable is born with a known state: no guessing, no garbage.

int score = 100; // copy initialization int score(100); // direct initialization int score{100}; // brace (uniform) initialization

What Is Assignment?

Assignment is the act of replacing a variable's current value with a new one after the variable already exists. The old value is discarded.

int score = 100; // initialization (first value) score = 200; // assignment (replaces 100 with 200)

The Lifecycle of a Value

time Initialization 100 Assignment 200 Assignment 350 int score = 100; score = 200; score = 350;

The Core Rule

Initialization
Occurs at declaration. Provides the first value. The variable has never existed before this line.
Assignment
Occurs after declaration. Overwrites the existing value. The variable must already exist.

Three Forms of Initialization

Copy
int x = 5;
Direct
int x(5);
Brace
int x{5};

Initialization vs. Assignment

Feature Initialization Assignment
When At declaration After declaration
Type name present? Yes No
Previous value? None (new variable) Overwritten
Syntax options =, (), {} = only
Works with const? Yes No

Why It Matters: const

A const variable can only be initialized, never assigned. If you forget to initialize it, the compiler will refuse to compile.

const int maxScore = 100; // ✓ initialization maxScore = 200; // ✕ ERROR: assignment to const const int limit; // ✕ ERROR: uninitialized const

Why It Matters: References

A reference must be initialized when declared. It is permanently bound to the object it references and cannot be rebound later.

int a = 10; int& ref = a; // ✓ initialization: ref bound to a int b = 20; ref = b; // assignment: changes a to 20, NOT rebinding int& bad; // ✕ ERROR: uninitialized reference

How to Apply This Knowledge

1
Read the line. Does it start with a type name (int, double, std::string, auto)?
2
If yes: this is a declaration. If a value is provided with =, (), or {}, that is initialization.
3
If no type name: the variable already exists. The = operator performs assignment.
4
Check constraints: is the variable const or a reference? If so, only initialization is allowed.
5
Prefer brace init: use {} for new variables to catch narrowing bugs at compile time.

Example 1: Identify Each Line

double temp = 98.6; // line A temp = 99.1; // line B double avg{98.85}; // line C avg = (temp + 98.6) / 2; // line D
Line A
Initialization: type double present, first value 98.6
Line B
Assignment: no type, replaces 98.6 with 99.1
Line C
Initialization: type double, brace syntax, first value 98.85
Line D
Assignment: no type, computes new value for avg

Example 2: Uninitialized Variables

int x; // declaration without initialization std::cout << x; // DANGER: undefined behavior!

When you declare a local variable without initializing it, the memory is not zeroed out. The variable holds whatever garbage bits were left there by previous code. Reading it is undefined behavior.

Example 3: Class Objects

std::string name("Alice"); // direct initialization name = "Bob"; // assignment std::vector<int> nums{1,2,3}; // list initialization nums = {4,5,6}; // assignment (replaces all)

Common Mistakes

✕ Confusing = in declaration
Seeing int x = 5; and calling it assignment. The = here is the initialization syntax, not the assignment operator.
✕ Forgetting to initialize
Declaring int count; and using it before giving it a value. This leads to undefined behavior and mysterious bugs.
✕ Assigning to const
Attempting const int k; k = 10; and expecting it to work. A const must be initialized on the same line.

Summary at a Glance

Scenario Code Init or Assign?
First value with = int x = 5; Initialization
First value with {} int x{5}; Initialization
Later change x = 10; Assignment
Const with value const int k = 7; Initialization
Const later k = 8; // ERROR Illegal
No value given int x; Uninitialized

Question 1 of 5

Which line performs initialization?

int a = 3; // line 1 a = 7; // line 2 int b; // line 3 b = 5; // line 4

Question 2 of 5

What will this code do?

const int limit; limit = 50;

Question 3 of 5

Which initialization form prevents narrowing conversions?

Question 4 of 5

After these two lines, what is the value of a?

int a = 10; int& ref = a; ref = 25;

Question 5 of 5

What is the best practice when declaring a local variable you intend to use immediately?