0
0
C++programming~10 mins

Scope of variables in C++ - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Scope of variables
Start Program
Declare Global Variable
Enter main()
Declare Local Variable
Use Variables
Enter Block (if/for)
Declare Block Variable
Use Variables in Block
Exit Block
Use Variables in main()
End Program
This flow shows how variables declared in different places have different visibility and lifetime during program execution.
Execution Sample
C++
int x = 10; // global

int main() {
  int x = 20; // local
  if (true) {
    int x = 30; // block
    // print x
  }
  // print x
}
This code shows three variables named x in different scopes: global, local in main, and block inside if.
Execution Table
StepScopeVariable x ValueActionOutput
1Global10Declare global x=10
2main() local20Declare local x=20
3if block30Declare block x=30
4if block30Print x inside block30
5Exit block20Block x destroyed, back to main local x
6main() local20Print x inside main20
7End10Global x still exists but not used here
💡 Program ends after main() finishes; block variable x is destroyed after block ends.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 5Final
Global x1010101010
main() local xN/A20202020
block xN/AN/A30N/AN/A
Key Moments - 3 Insights
Why does the print inside the block show 30, not 20 or 10?
Because inside the block, the block variable x=30 shadows the local and global x. See execution_table step 4.
After the block ends, which x is used when printing?
The block variable x is destroyed after the block ends, so the local x=20 in main() is used again. See execution_table step 6.
Is the global x accessible inside main()?
Yes, but it is shadowed by the local x=20, so using x refers to the local one inside main(). See variable_tracker for values.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of x printed inside the if block (step 4)?
A20
B30
C10
DUndefined
💡 Hint
Check execution_table row with step 4 under 'Output' column.
At which step does the block variable x get destroyed?
AStep 5
BStep 3
CStep 6
DStep 7
💡 Hint
Look at execution_table step 5 description about exiting the block.
If we remove the local x=20 in main(), what value would be printed at step 6?
A30
B20
C10
DError
💡 Hint
Without local x, main() uses global x=10. See variable_tracker for global x value.
Concept Snapshot
Scope of variables in C++:
- Global variables declared outside functions are accessible everywhere unless shadowed.
- Local variables declared inside functions hide global variables with same name.
- Block variables declared inside blocks (if, for) hide outer variables and exist only inside block.
- When block ends, block variables are destroyed and outer variables become visible again.
- Variable name resolution uses closest scope first.
Full Transcript
This visual execution shows how variables named x declared in different scopes behave in C++. The global x=10 exists throughout the program. Inside main(), a local x=20 hides the global x. Inside the if block, a block x=30 hides both local and global x. Printing inside the block shows 30. After the block ends, the block x is destroyed, so printing in main() shows 20. The global x remains but is shadowed by local variables. This demonstrates variable scope and shadowing clearly.