Interview Q&A · C · 8 questions in this topic
Scope, Linkage & Storage Duration — C Interview Q&A
Where names are visible, how long objects live, and the linkage rules behind multi-file programs. Part of Functions & Storage · 202 questions in the C bank.
What is the difference between scope, linkage and storage duration?
Answer
Scope is where a name is visible (block, file, function, prototype). Linkage is whether declarations in different scopes refer to the same entity (external, internal, none). Storage duration is how long the object lives (automatic, static, allocated, thread).
One declaration has all three properties: a file-scope static int has file scope, internal linkage, static duration. Interviews test whether you keep the three axes separate.
Key points
- 1Scope — visibility of the name.
- 2Linkage — identity across scopes.
- 3Duration — lifetime of the object.
💡 Name one example combining all three axes — that proves the model, not just the vocabulary.
What is a translation unit and what breaks when headers define things?
Answer
A translation unit is one .c file after preprocessing — the compiler input. Headers are pasted into TUs, so any non-static definition in a header (function body, global variable) repeats in every includer and fails at link time with multiple definition.
Headers must carry declarations, macros, type definitions and static inline functions only; definitions live in exactly one .c file.
Example
/* BAD in a header: int counter = 0; // repeats per TU */ /* GOOD in a header: extern int counter; // declaration only */ /* GOOD in one .c file: int counter = 0; // single definition */
What is tentative definition? (`int x;` at file scope in two files.)
Answer
A file-scope declaration without initializer or storage specifier is tentative: it may be completed later in the same TU, and historically linkers merged duplicates across TUs (common symbols). Since GCC 10 (-fno-common default), duplicates across TUs are now multiple-definition errors like any other.
Modern rule: exactly one definition with an initializer; all other TUs use extern declarations.
Example
/* a.c: int shared = 0; // the single definition */ /* b.c: extern int shared; // declaration, no storage */
💡 Mention -fno-common becoming the default — it shows you track toolchain behavior, not just the standard.
What are global variables good and bad for?
Answer
Good for genuine program-wide state (configuration, single hardware handles) with internal linkage (static) where possible. Bad as casual communication between functions: hidden coupling, untestable ordering, thread-unsafety, and initialization-order surprises.
Prefer parameter passing; when globals are justified, gate them behind accessor functions that can enforce invariants and add locking later.
Example
/* config.c: controlled global */ static int log_level = 1; void set_log_level(int l) { log_level = l; } int get_log_level(void) { return log_level; }
What is shadowing and why do compilers warn about it?
Answer
Shadowing redeclares an outer name in an inner scope — the inner declaration hides the outer within its block. It compiles but usually signals confusion: assignments hit the wrong variable, and -Wshadow exists because real bugs (especially loop counters and parameter shadowing) hide behind it.
Treat -Wshadow findings as renames waiting to happen, not noise.
Example
int level = 1; void f(void) { /* int level = 2; // shadows global: assignments miss the global */ }
What is the lifetime of function parameters, locals, globals and heap objects?
Answer
Parameters and block locals: automatic — born at entry, dead at exit (returning their address dangles). Globals and file statics: static — born before main, dead after exit, zero-initialized. malloc results: allocated — alive until free, whenever that is. VLAs: automatic with runtime size, freed at scope exit (no manual free, but stack-overflow risk).
Every use-after-lifetime bug is one of these four categories misjudged.
Key points
- 1Automatic — scope-bound, indeterminate initial value.
- 2Static — program-bound, zero-initialized.
- 3Allocated — free-bound, programmer-managed.
Example
int *bad(void) { int local = 1; /* return &local; // dangling: frame dies at return */ return NULL; }
What is reentrancy and which C functions are async-signal-safe?
Answer
A reentrant function can be safely re-entered (recursion, signals, threads) because it uses only local state. Async-signal-safe functions (a small POSIX list: write, _exit, sigaction-safe helpers) may be called from signal handlers; printf/malloc/free are not — they hold locks and global state a handler would corrupt.
Handlers should set a volatile sig_atomic_t flag and return; the main loop does the real work.
Example
/* signal handler: minimal and safe */ volatile sig_atomic_t stop = 0; void on_term(int sig) { (void)sig; stop = 1; } /* flag only, no printf */
How do you organize a multi-file C program (headers, source, private state)?
Answer
One module = one header (public API: prototypes, opaque-pointer types, error codes) plus one .c file (definitions, file-static helpers and state). Headers are self-contained (include what they use) and guarded; private state stays static in the .c file, exposed only through functions.
Opaque pointers (struct declared, never defined, in the header) give C its encapsulation story.
Example
/* sensor.h */ #ifndef SENSOR_H #define SENSOR_H typedef struct sensor sensor_t; /* opaque: internals hidden */ sensor_t *sensor_open(int id); void sensor_close(sensor_t *s); #endif
💡 Opaque pointer plus accessor functions is the C encapsulation pattern — name it explicitly.
Recommended resourcesAffiliate links · may earn us a commission