0
0
Cprogramming~10 mins

Scope of variables - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Scope of variables
Start Program
Declare Global Variable
Enter Function
Declare Local Variable
Use Variables
Exit Function
End Program
This flow shows how variables declared globally and locally are accessed and used inside and outside functions.
Execution Sample
C
#include <stdio.h>
int x = 10; // global

void func() {
  int x = 5; // local
  printf("%d\n", x);
}

int main() {
  func();
  printf("%d\n", x);
  return 0;
}
This code shows a global variable x and a local variable x inside func(), printing both to show scope differences.
Execution Table
StepActionVariable x ValueOutput
1Program starts, global x declared10 (global)
2Enter func(), local x declared5 (local shadows global)
3Print x inside func()5 (local)5
4Exit func(), local x destroyed10 (global remains)
5Print x inside main()10 (global)10
6Program ends10 (global)
💡 Program ends after printing local and global x values showing scope difference.
Variable Tracker
VariableStartInside func() startInside func() printAfter func() endmain() printEnd
x10 (global)5 (local shadows global)5 (local)10 (global)10 (global)10 (global)
Key Moments - 3 Insights
Why does the local variable x inside func() not change the global x?
Because the local x declared inside func() creates a new variable that only exists during func() execution, shadowing the global x. See execution_table steps 2 and 4.
What value of x is printed inside func() and why?
The value 5 is printed because inside func(), the local x (value 5) shadows the global x (value 10). See execution_table step 3.
Why does main() print the global x value 10 after func() ends?
Because the local x inside func() is destroyed after func() ends, so main() accesses the global x which remains 10. See execution_table steps 4 and 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of x printed inside func() at step 3?
A5
B10
CUndefined
D0
💡 Hint
Check the 'Output' column at step 3 in the execution_table.
At which step does the local variable x get destroyed?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at the 'Action' column describing variable scope changes in execution_table.
If the local variable x inside func() was not declared, what would func() print?
A5
B0
C10
DCompilation error
💡 Hint
Refer to variable_tracker and understand which x is accessed if local x is missing.
Concept Snapshot
Scope of variables in C:
- Global variables declared outside functions are accessible everywhere.
- Local variables declared inside functions exist only during that function's execution.
- Local variables can shadow global variables with the same name.
- After function ends, local variables are destroyed.
- Accessing a variable uses the closest scope first.
Full Transcript
This visual trace shows how variable scope works in C. A global variable x is declared with value 10. Inside the function func(), a local variable x with value 5 is declared, which shadows the global x. When func() prints x, it shows 5. After func() ends, the local x is destroyed, and main() prints the global x value 10. This demonstrates that local variables exist only inside their function and do not affect global variables with the same name.