The VikiD Truth

Knowledge base / Computing / C & systems languages

The plain idea, up front

Four C concepts, scope, duration (lifetime), linkage and storage class, are endlessly confused because they overlap and sound alike. The one-line summary: scope is where a name is visible, duration is how long the data lives, linkage is whether the same name in different files means the same thing, and storage class specifiers (auto, static, extern, register) are the keywords you use to control the first three. This page untangles them one at a time.

Why it matters

Getting these wrong produces some of C’s most baffling bugs: a variable that mysteriously keeps its value between calls, a “multiple definition” linker error, a name that is visible where you did not expect, or memory that vanishes while you are still using it. Once the four ideas are separated in your head, static and extern stop being mysterious and the linker’s complaints start making sense.

An everyday analogy

Think of a company. Scope is which rooms a person is allowed into. Duration is how long they are employed. Linkage is whether “Sam in Accounts” and “Sam” mentioned in another department are the same Sam. Storage class is the type of contract (permanent, temporary, on-loan) that sets all of that. A person can be permanently employed (duration) but only allowed in one room (scope), which is exactly what static does to a variable.

Sorting the Confusion between Storage Classes, Linkage, Duration & Scope

Back to C Programming

There are four distinct ideas that we need to master:

Objects have storage duration, not linkage; and identifiers have linkage, not storage duration.

Duration

There are two kinds of duration:

Storage Classes

During the execution of a program, each object exists as a physical location in memory for a certain period, called its lifetime. The lifetime is determined by its storage duration which have corresponding storage class specifiers.

There are four kinds of storage duration static, thread, automatic and allocated.

Typically

The are five storage class specifiers: static, extern, auto, register and typedef.

Automatic Duration

Each declaration can only have a single storage class specifier.

Linkage

There are three kinds of linkage:

Sources

A worked example: the many faces of static

int  globalCounter;          // external linkage, static duration, file scope
static int fileCounter;      // INTERNAL linkage: private to this file

void tick(void) {
    static int calls = 0;    // static duration, but block scope:
    calls++;                 //   keeps its value between calls,
    int local = calls;       //   while `local` is created fresh each call
}

extern int globalCounter;    // a *declaration*: "defined elsewhere"

Notice static means two different things depending on where it sits: at file level it hides a name (internal linkage); inside a function it makes a variable persist for the whole program (static duration) while staying privately scoped to that block. That double meaning is the single biggest source of confusion, and seeing it laid out is half the battle.

Common misconceptions

How it connects

The truth revealed: C’s storage rules only look tangled because four separate ideas share a handful of keywords, and once you ask of every variable “where, how long, and is it shared?”, the confusion dissolves.

Last edited · 31 revisions

Tags: #Computing