0
0
Cprogramming~10 mins

Lifetime and scope comparison - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Lifetime and scope comparison
Start Program
Declare Variables
Enter Function
Check Variable Scope
Local
Lifetime & Scope Behavior
End Program
This flow shows how variables are declared and how their lifetime and scope differ depending on whether they are local, global, or static.
Execution Sample
C
#include <stdio.h>
int global_var = 10;

void func() {
    int local_var = 5;
    static int static_var = 0;
    static_var++;
    printf("%d %d %d\n", global_var, local_var, static_var);
}

int main() {
    func();
    func();
    return 0;
}
This code shows global, local, and static variables and how their values change or reset across function calls.
Execution Table
StepVariableValueScopeLifetimeActionOutput
1global_var10GlobalWhole programInitialized before main
2func() call 1Enter func
3local_var5Local to funcDuring func callInitialized
4static_var0Static localWhole programInitialized once
5static_var1Static localWhole programIncremented
6OutputPrint values10 5 1
7func() call 1Exit func, local_var destroyed
8func() call 2Enter func again
9local_var5Local to funcDuring func callInitialized again
10static_var1Static localWhole programRetains previous value
11static_var2Static localWhole programIncremented
12OutputPrint values10 5 2
13func() call 2Exit func, local_var destroyed
14Program Endmain returns, program ends
💡 Program ends after main returns; local variables destroyed after each func call; static and global variables persist.
Variable Tracker
VariableStartAfter func call 1After func call 2Final
global_var10101010
local_varN/A5 (exists during call)5 (exists during call)N/A (destroyed after calls)
static_var0122
Key Moments - 3 Insights
Why does static_var keep its value between function calls but local_var does not?
Because static_var has static lifetime and is initialized only once, it keeps its value across calls. local_var is created and destroyed each time func runs, so it resets (see execution_table rows 4-7 and 9-13).
Does global_var change during the program execution?
No, global_var is initialized once before main and remains unchanged throughout (see execution_table rows 1, 6, and 12).
When exactly is local_var created and destroyed?
local_var is created at the start of each func call and destroyed when func returns (see execution_table rows 3 and 7 for first call, 9 and 13 for second).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 5, what is the value of static_var?
A5
B0
C1
D10
💡 Hint
Check the 'Value' column for static_var at step 5 in execution_table.
At which step does local_var get destroyed after the first func call?
AStep 3
BStep 7
CStep 6
DStep 9
💡 Hint
Look for the action mentioning local_var destruction after func call 1 in execution_table.
If static_var was not static, what would be its value at step 11?
A1
B2
C0
DUndefined
💡 Hint
Consider that non-static local variables are reinitialized each call (see variable_tracker for static_var changes).
Concept Snapshot
Lifetime and Scope in C:
- Global variables: scope whole program, lifetime entire run
- Local variables: scope inside function/block, lifetime during call
- Static local variables: scope local, lifetime entire program
- Static variables keep value between calls
- Local variables reset each call
Full Transcript
This example compares lifetime and scope of variables in C. Global variables exist and keep their value throughout the program. Local variables exist only during a function call and are destroyed after. Static local variables behave like locals in scope but keep their value between calls because their lifetime is the whole program. The execution table shows how static_var increments across calls, local_var resets, and global_var stays constant. Key moments clarify why static_var retains value and when local_var is created and destroyed. The quiz tests understanding of these concepts by referencing specific steps and variable values.