Lesson 3 of 8
Scope, Lifetime & Linkage
In C, every variable has three properties: **scope** (where the name is visible), **lifetime** (how long the storage persists), and **linkage** (whether the name is visible across translation units). Getting these wrong causes some of the most insidious bugs: stale function-static state, accidental global variable sharing, and dangling references.
Scope & Lifetime - Three Zones of a C Program
#include <stdio.h>
/* ── File scope (global) - visible everywhere in this .c file ─
Lifetime: entire program run.
Linkage: external by default (visible to other .c files). */
int global_counter = 0; /* initialized, goes in .data segment */
int uninit_global; /* zero-initialized, goes in .bss segment */
/* ── static at file scope - file-local (private) ──────────── */
static int file_private = 42; /* NOT visible outside this .c file */
void increment(void) {
/* ── Block scope (local) - visible inside this { } only ──
Lifetime: duration of the function call.
Linkage: none (cannot be referenced by name from outside). */
int local = 10; /* on stack, dies when function returns */
global_counter++;
local++;
printf("local=%d, global=%d
", local, global_counter);
}
/* ── static LOCAL variable - persists between calls ────────── */
void count_calls(void) {
/* 'static' inside a function = same storage as global (BSS/data),
but scope is still local to the function.
Initialized ONCE at program start, not on each call. */
static int call_count = 0; /* persists between calls! */
call_count++;
printf("count_calls called %d time(s)
", call_count);
}
/* ── Shadowing - inner name hides outer ────────────────────── */
void shadow_demo(void) {
int x = 1;
printf("outer x = %d
", x); /* 1 */
{
int x = 2; /* shadows the outer x inside this block */
printf("inner x = %d
", x); /* 2 */
}
printf("outer x = %d
", x); /* 1 - inner x is gone */
}
int main(void) {
increment(); /* local=11, global=1 */
increment(); /* local=11, global=2 */
count_calls(); /* called 1 time(s) */
count_calls(); /* called 2 time(s) */
shadow_demo();
printf("file_private=%d
", file_private);
return 0;
}Common Mistakes
- Using static local variables for shared state in multi-threaded code - static locals are NOT thread-safe. Two threads calling the same function simultaneously share the same `static` variable without synchronisation.
- Accidentally relying on global variable zero-initialisation - uninitialised globals are zero in C, but this is a language rule, not something to rely on for clarity. Explicit initialisation communicates intent.
- Name collision between a local and a global - `int counter` inside a function shadows the global `counter`. Bugs occur when you think you're updating the global but you're updating the local copy. Always name globals distinctly.
Tip
Tip
Practice Scope Lifetime Linkage in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Variable visibility and lifetime depend on declaration placement and storage class specifiers
Practice Task
Note
Practice Task - (1) Write a working example of Scope Lifetime Linkage from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Scope Lifetime Linkage is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready c code.
Key Takeaways
- In C, every variable has three properties: **scope** (where the name is visible), **lifetime** (how long the storage persists), and **linkage** (whether the name is visible across translation units).
- Using static local variables for shared state in multi-threaded code - static locals are NOT thread-safe. Two threads calling the same function simultaneously share the same `static` variable without synchronisation.
- Accidentally relying on global variable zero-initialisation - uninitialised globals are zero in C, but this is a language rule, not something to rely on for clarity. Explicit initialisation communicates intent.
- Name collision between a local and a global - `int counter` inside a function shadows the global `counter`. Bugs occur when you think you're updating the global but you're updating the local copy. Always name globals distinctly.
Finished this lesson?