Interview Q&A · C · 8 questions in this topic

Scope, Linkage & Storage Duration — C Interview Q&A

All C topics

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

  1. 1Scope — visibility of the name.
  2. 2Linkage — identity across scopes.
  3. 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

Example.cc
/* 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

Example.cc
/* 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.

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits