Bird
Raised Fist0
C++programming~10 mins

Variable declaration and initialization in C++ - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Variable declaration and initialization
Start
↓
Declare variable
↓
Initialize variable
↓
Use variable
↓
End
First, we declare a variable to reserve space. Then we give it a value (initialize). Finally, we can use it in the program.
Execution Sample
C++
int age = 25;
int height;
height = 180;
int sum = age + height;
Declares variables 'age', 'height', and 'sum'. Initializes 'age' and 'height', then calculates 'sum'.
Execution Table
StepActionVariableValueExplanation
1Declare and initialize 'age'age25'age' is created and set to 25
2Declare 'height'heightundefined'height' is created but not initialized yet
3Initialize 'height'height180'height' is assigned the value 180
4Declare and initialize 'sum'sum205'sum' is created and set to age + height (25 + 180)
5End--All variables declared and initialized
💡 Program ends after all variables are declared and initialized
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
ageundefined2525252525
heightundefinedundefinedundefined180180180
sumundefinedundefinedundefinedundefined205205
Key Moments - 2 Insights
Why is 'height' undefined after declaration but before initialization?
Because declaring a variable reserves space but does not assign a value until initialization (see Step 2 in execution_table).
Can we use a variable before initializing it?
No, using an uninitialized variable can cause errors or unexpected values. Initialization must happen first (see Step 3 and 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'age' after Step 3?
A180
B25
Cundefined
D205
💡 Hint
Check the 'age' value column at Step 3 in the execution_table.
At which step does 'sum' get its value?
AStep 2
BStep 3
CStep 4
DStep 1
💡 Hint
Look for when 'sum' is declared and initialized in the execution_table.
If we skip initializing 'height', what would be the value of 'sum' at Step 4?
Aundefined + 25
BCompilation error
C25 + 0
D205
💡 Hint
Using uninitialized variables leads to undefined behavior in C++; see variable_tracker where 'height' is 'undefined'.
Concept Snapshot
Variable declaration reserves space: e.g., int x;
Initialization assigns value: e.g., x = 5;
You can combine: int x = 5;
Use variables only after initialization to avoid errors.
Variables hold data used in calculations or logic.
Full Transcript
This example shows how variables are declared and initialized in C++. First, 'age' is declared and given the value 25. Then 'height' is declared but not initialized, so it holds an undefined value. Next, 'height' is initialized to 180. Finally, 'sum' is declared and initialized as the sum of 'age' and 'height', which is 205. Variables must be declared before use, and initialized before using their values to avoid errors.

Practice

(1/5)
1. Which of the following correctly declares and initializes an integer variable named age with the value 25 in C++?
easy
A. int age = 25;
B. age int = 25;
C. int = 25 age;
D. int age;

Solution

  1. Step 1: Understand variable declaration syntax

    In C++, the type comes first, then the variable name, followed by an optional initialization with '='.
  2. Step 2: Check each option

    int age = 25; follows the correct syntax: int age = 25;. Others have wrong order or missing initialization.
  3. Final Answer:

    int age = 25; -> Option A
  4. Quick Check:

    Type + name + = value = correct declaration [OK]
Hint: Type first, then name, then = value for initialization [OK]
Common Mistakes:
  • Putting variable name before type
  • Missing semicolon
  • Trying to assign value before declaring type
2. Which of the following is a syntax error in C++ variable declaration?
easy
A. float price = 9.99;
B. char letter = 'A';
C. int 2ndNumber = 10;
D. bool isReady = true;

Solution

  1. Step 1: Identify valid variable names

    Variable names cannot start with a digit in C++.
  2. Step 2: Check each option

    int 2ndNumber = 10; uses 2ndNumber which starts with '2', invalid. Others are correct declarations.
  3. Final Answer:

    int 2ndNumber = 10; -> Option C
  4. Quick Check:

    Variable names cannot start with digits [OK]
Hint: Variable names must start with letter or underscore [OK]
Common Mistakes:
  • Starting variable name with a number
  • Using spaces in variable names
  • Missing quotes for char values
3. What is the output of the following C++ code?
int x = 5;
int y = x + 3;
std::cout << y << std::endl;
medium
A. 3
B. 8
C. 5
D. Compilation error

Solution

  1. Step 1: Understand variable initialization and addition

    Variable x is 5, y is initialized as x + 3, so 5 + 3 = 8.
  2. Step 2: Output the value of y

    The code prints y, which is 8.
  3. Final Answer:

    8 -> Option B
  4. Quick Check:

    5 + 3 = 8 [OK]
Hint: Add values assigned to variables before printing [OK]
Common Mistakes:
  • Confusing variable names
  • Forgetting to add values
  • Assuming uninitialized variables
4. Identify the error in the following C++ code snippet:
int number;
number = 10
std::cout << number << std::endl;
medium
A. Missing semicolon after assignment
B. Variable 'number' not declared
C. Cannot assign value after declaration
D. std::cout syntax is incorrect

Solution

  1. Step 1: Check syntax after assignment

    In C++, each statement must end with a semicolon. The line number = 10 is missing a semicolon.
  2. Step 2: Verify other parts

    Variable is declared correctly, assignment after declaration is allowed, and std::cout syntax is correct.
  3. Final Answer:

    Missing semicolon after assignment -> Option A
  4. Quick Check:

    Statements must end with semicolon [OK]
Hint: Check for missing semicolons after each statement [OK]
Common Mistakes:
  • Forgetting semicolon after assignment
  • Assuming declaration and assignment must be together
  • Misusing std::cout syntax
5. You want to declare and initialize three variables in one line: an integer a with 10, a float b with 5.5, and a boolean c as true. Which of the following is correct in C++?
hard
A. int a = 10, float b = 5.5, bool c = true;
B. int a = 10; int b = 5.5; int c = true;
C. int a = 10, b = 5.5, c = true;
D. int a = 10; float b = 5.5; bool c = true;

Solution

  1. Step 1: Understand type declarations in one line

    In C++, variables of different types cannot be declared in a single statement separated by commas. Each type needs its own statement.
  2. Step 2: Check each option

    int a = 10; float b = 5.5; bool c = true; declares each variable separately with correct types and initialization. int a = 10, float b = 5.5, bool c = true; tries to mix types in one statement, which is invalid. int a = 10, b = 5.5, c = true; declares all as int, which is wrong for float and bool. int a = 10; int b = 5.5; int c = true; declares all as int, which is incorrect types.
  3. Final Answer:

    int a = 10; float b = 5.5; bool c = true; -> Option D
  4. Quick Check:

    Different types need separate declarations [OK]
Hint: Declare different types in separate statements [OK]
Common Mistakes:
  • Mixing types in one declaration line
  • Assigning float or bool to int variables
  • Missing semicolons between declarations
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard