Scope, lifetime & storage classes
10 min lesson
- Scope is the part of the program where a name is visible, and lifetime is how long a variable exists in memory during the run.
- These ideas matter because they explain why a variable can be 'unknown' in one place and why some values survive between function calls while others do not.
- Think of scope as the rooms a name can be heard in: a name declared in the kitchen is not heard in the bedroom.
- A block is a set of braces { ... }. A local variable is declared inside a block and is visible only there.
- A global variable is declared outside all functions and is visible to every function after its declaration.
- Lifetime describes when a variable exists: local variables live from their declaration until the block ends; globals live for the whole program.
- Storage classes control both: auto (default for locals), static (keeps its value between calls, or gives a file-scope global internal linkage), extern (declares a name defined elsewhere), and register (a hint to keep a variable in a CPU register).
- The computer allocates local variables on the call stack when a function runs and releases them when it returns, while globals and statics live in a fixed memory area for the whole program.
- The syntax is: static int count = 0; inside a function to keep count between calls, or extern int shared; to refer to a global defined in another file.
- A realistic use: a static counter that tracks how many times a function has been called, or a global configuration value shared by many functions.
- A surprising detail: a static local variable is initialized only once, on the first call, and keeps its value on later calls.
- The example program uses a static local counter to count function calls and shows how a local variable is invisible outside its block.
- Keep variables as local as possible; narrow scope reduces bugs.
- Use static inside a function when you need a value to persist between calls.
- Avoid globals unless truly shared; they make code harder to reason about.
- Remember that a local variable disappears when its block ends.
- Understanding scope makes debugging much easier. Great progress!
#include <stdio.h>
// A function that counts how many times it has been called
int nextCount(void) {
// static local: created once, keeps its value between calls
static int count = 0;
// Increase the counter each call
count++;
// Return the current count
return count;
}
int main(void) {
// A local variable visible only inside main
int localValue = 42;
// Print it once
printf("Local: %d\n", localValue);
// Call nextCount three times to show the static counter grows
printf("Call 1: %d\n", nextCount());
printf("Call 2: %d\n", nextCount());
printf("Call 3: %d\n", nextCount());
{
// A block-scoped variable, invisible outside these braces
int inner = 7;
printf("Inner: %d\n", inner);
}
return 0;
}What happens: Local: 42 Call 1: 1 Call 2: 2 Call 3: 3 Inner: 7
Breaking it down
static local
static int count = 0;Created once, on the first call, and keeps its value between calls. Without static, count would reset to 0 every time.
Increment
count++;Adds 1 to the persistent counter each call, so the returned values are 1, 2, 3 across calls.
Local in main
int localValue = 42;Exists only while main runs and is invisible to other functions. Other functions cannot see or change it.
Inner block
{ int inner = 7; ... }The variable inner lives only inside its braces. After the closing brace it no longer exists.
Storage classes at a glance
| Class | Where used | Lifetime | Default value |
|---|---|---|---|
| auto | Inside blocks (default) | Until block ends | Garbage |
| static (local) | Inside functions | Whole program | Zero |
| static (global) | File scope | Whole program | Zero |
| extern | Anywhere | Depends on definition | N/A |
| register | Inside blocks | Until block ends | Garbage |
A helpful analogy
Scope is like sound in a house. A name spoken in the kitchen (a block) is only heard in the kitchen. A name announced over the house intercom (a global) is heard everywhere. Lifetime is how long the speaker is on: a local speaker is switched off when the room empties, but the intercom stays on all day.
Nesting rules
- 1.A local variable shadows a global with the same name inside its block.
- 2.A static local is initialized only once, even if the function is called many times.
- 3.Globals and statics are zero-initialized by default; locals are not, so initialize them.
- 4.Use extern in a header to share a global defined in another source file.
Common mistakes
Using a local variable outside its block
Avoid
{ int x = 5; }
printf("%d", x); // unknownUse instead
int x = 5;
printf("%d", x);The variable x exists only inside the braces. Once the block ends, the name is no longer visible and the compiler errors.
Forgetting static on a counter
Avoid
int count = 0; // resets each callUse instead
static int count = 0; // persistsWithout static the counter is recreated and reset on every call, so it always returns 1.
Relying on uninitialized locals
Avoid
int x;
printf("%d", x); // garbageUse instead
int x = 0;
printf("%d", x);Local variables are not zero-initialized. Reading one before assigning gives unpredictable values.
Quick tips
- •Declare variables in the smallest block that needs them.
- •Prefer static locals over globals for state that only one function uses.
- •Name globals with a clear prefix like g_ so they stand out.
- •Try removing static from the counter and run the example to see it reset.
- •Use -Wshadow to catch accidental shadowing of outer variables.