C++ · Variables and types
Constants, variables and assignment
Distinguish C++ initialization from assignment, trace copied and overwritten values, choose suitable types and try self-checks before revealing answers.
Practice this topic in the app Read the lesson in the app
Variables, types and assignment
A variable is like a labelled box: int score = 80; creates an integer variable named score and initializes it to 80.
A declaration tells the compiler the type and name. Initialization gives a new variable its first value; a later score = 90; is assignment. score = score + 5; first calculates with the old value and then stores the result. It is not a mathematical equality.
Assignment normally copies the current value. After int b = a;, changing a does not change b. To swap values, first preserve the value that would otherwise be overwritten in a temporary variable. A fixed value can be declared as const int DAYS = 7; and cannot be assigned to afterwards.
Choose a type, then trace each assignment
int commonly stores integers, double stores floating-point approximations, char represents one character, and bool represents true or false. Types affect both storage and expression evaluation. Integer ranges depend on the implementation; double cannot represent every decimal exactly.
Initialization gives a new variable its first value; assignment updates an existing variable. Predict a and b before tracing these statements:
| After | a | b |
|---|---|---|
| int a = 4; | 4 | Not declared |
| int b = a; | 4 | 4 |
| a = a + 3; | 7 | 4 |
b copies the value at that moment; it does not stay linked to a. a = a + 3 reads the old value 4, computes 7, then overwrites a. Initialize an ordinary local variable before reading it.
Run and check: copy the current value
#include <iostream>
int main() {
int a = 4;
int b = a;
a = a + 3;
std::cout << a << " " << b << "\n";
}Output: 7 4. Updating a does not assign a new value to b.
Try before revealing each answer
Next execute b = a; a = 1;. What are a and b?
a is 1 and b is 7. b copied a at the time of the second assignment.
Does a = 3; b = 8; a = b; b = a; swap the values?
No: both end as 8. The first assignment overwrites 3; preserve the old value in a temporary variable.
Which types could store a book page count and whether it is finished?
Use an int with a suitable range for the page count and bool for whether it is finished. Choose types by meaning and range.
Assignment and swapping examples
Example 1: trace assignments line by line
#include <iostream>
using namespace std;
int main() {
int a = 6;
int b = a;
a = a + 4;
cout << a << " " << b << endl;
}The output is 10 6. b stores the value 6 from its initialization; it is not an alias for a.
Example 2: preserve the old value before swapping
#include <iostream>
using namespace std;
int main() {
int a = 3, b = 8;
int oldA = a;
a = b;
b = oldA;
cout << a << " " << b << endl;
}The output is 8 3. Writing a=b; b=a; directly would make both values 8, losing the original 3.
Tips
- Reading an uninitialized ordinary local variable can cause undefined behavior. Do not assume its value is zero.
- The left side of an assignment must be modifiable. You cannot write 3 = a.
- You cannot define the same name twice in one scope. Variable names are case-sensitive.
Technical references
Scope note
This lesson is written by InfoCode and is not an official syllabus; the reference level is a site mapping and does not represent official levels, certification or eligibility. Practice records do not establish mastery or eligibility.
Reference level: GESP Level 1 (site mapping)