YOUR C LEARNING COMPANION

Find your next idea.

Learning path
Language detailsChapter 17 of 73

Storage classes: duration, scope, and linkage

Ask separate questions about names, object lifetimes, definitions, initialization, and shared declarations.

8 min readFoundationsC17 + C23 notes
Offline reading
In this chapter10 sections

Where can a name be used? How long does its object exist? Can another source file refer to the same entity by name? These are separate questions. Storage-class specifiers participate in the rules that answer them.

1. Ask four different questions

QuestionConceptExample
Where in the source can I use this name?ScopeA block-local identifier is visible within its scope.
Which declarations name the same entity?LinkageCompatible extern declarations can name one shared object.
During which execution period does this object exist?Lifetime / storage durationA static local outlives an individual function call.
What value does it start with?InitializationAn uninitialized static int starts at zero.

A Storage class specifier participates in these declaration rules; its name is not a literal promise of a hardware storage location. Keep the questions separate even when one keyword affects several answers.

2. Automatic locals restart; static locals retain state

Complete C17 program
#include <assert.h>

static int next_value(void)
{
    int fresh = 10;
    static int calls = 0;
    ++calls;
    return fresh + calls;
}

int main(void)
{
    int first = next_value();
    int second = next_value();
    assert(first == 11 && second == 12);
    return 0;
}

Each call creates a fresh automatic fresh, initialized to 10. The static calls object is initialized once before program startup and survives between calls. The results are 11 and 12. Its name still has block scope and no linkage; a long lifetime does not make the name global.

For an ordinary automatic object that is not a variable length array, lifetime runs from entry to the associated block until that block execution ends. A VLA's lifetime starts at its declaration and ends when execution leaves its declaration's scope. Recursion can create simultaneous automatic instances. These are language rules, not a universal requirement to use a physical “stack.”

Static and thread-duration objects without explicit initializers receive typed zero initialization: arithmetic zero, null pointers, and recursively initialized members. Ordinary uninitialized automatic objects do not gain usable zero values. Avoid reading them before initialization.

C17 · illustrative fragment
static int total;
static int *target;

Here total is zero and target is a null pointer. A null pointer need not have an all-zero byte representation.

3. static and extern answer linkage questions in context

At file scope, static int total; gives total internal linkage and static storage duration. Other translation units cannot use that identifier's linkage to name the same object, but the object could still be exposed through a valid pointer interface. Inside a block, static int total; instead declares a block-scope object with no linkage and static duration.

extern does not mean “created somewhere else” or “no storage.” A file-scope declaration such as extern int total = 1; is a definition. An uninitialized extern int total; commonly supplies a declaration of an object defined once elsewhere.

Complete C17 program
#include <assert.h>

static int total;

static void record(void)
{
    extern int total;
    ++total;
}

int main(void)
{
    record();
    record();
    assert(total == 2);
    return 0;
}

The block extern declaration inherits the internal linkage of the visible file-scope declaration. It names the same total; it does not create a second object or make the first externally linked. If no prior declaration with linkage is visible, the relevant extern rules normally establish external linkage. Prefer a simple consistent declaration policy over clever redeclarations.

A file-scope int total; is a Tentative definition in C17, not an instruction to create an independent instance in every file. See translation units and linkage before distributing object declarations across headers.

4. Optional: one definition shared by two source files

Read with the Building programs unit

The header declares the shared object and function. counter.c defines them. local_calls has internal linkage; client.c refers to codedile_total through the shared declaration. These are separate translation units, not one pasted program.

C17 · project file · counter.h
#ifndef CODEDILE_COUNTER_H
#define CODEDILE_COUNTER_H

extern unsigned codedile_total;
void codedile_record(void);

#endif
C17 · project file · counter.c
#include "counter.h"

unsigned codedile_total = 0;
static unsigned local_calls = 0;

void codedile_record(void)
{
    ++local_calls;
    codedile_total = local_calls;
}
C17 · project file · client.c
#include <assert.h>
#include "counter.h"

int main(void)
{
    codedile_record();
    codedile_record();
    assert(codedile_total == 2);
    return 0;
}

The two calls produce a shared total of 2. This bounded example is single-threaded and makes only two increments. If shared state is accessed by threads or the counter can reach its limit, the module needs explicit synchronization and range policies. A private name alone supplies neither.

5. auto, register, typedef, and thread duration

  • auto int value = 3; explicitly requests the ordinary automatic local case in C17. Usually write int value = 3;.
  • register is an optimization suggestion, not guaranteed CPU-register placement or speed. In C17 and C23, direct unary & cannot take the address of a register-declared object. Register arrays also have special restrictions; omit register in ordinary teaching interfaces.
  • typedef is in the grammar's storage-class-specifier category for syntactic convenience. It creates a type alias, not an object or a new storage duration.
  • _Thread_local declares one thread-duration object per thread. At block scope it must accompany static or extern; every declaration of that object must consistently include it. At file scope, static/extern can additionally choose linkage.
A thread-duration declaration, not a multi-thread execution demo
C17 · translation-unit fragment
static _Thread_local unsigned thread_visits;

unsigned visit_in_this_thread(void)
{
    return ++thread_visits;
}

Each thread has its own thread_visits initialized to zero. The function operates on the calling thread's instance. Accessing another thread's object indirectly is implementation-defined; thread duration does not make pointer sharing automatically safe. Thread support is an implementation capability to check.

Intentional diagnostic: address of a register object
C17 · intentional build rejection
int main(void)
{
    register int value = 7;
    int *address = &value;
    return *address;
}

The diagnostic follows a source-language constraint even if the compiler would otherwise store value in addressable memory.

6. The same spelling can have another role

In an array parameter, int items[static 4] states a minimum accessible-element contract for each call; the parameter is adjusted to a pointer type. It does not allocate a static local array. Read arrays and pointers together before using this form.

C23 also uses auto for type inference in supported initialized declarations, for example auto count = 3;. It adds the primary spelling thread_local and the specifier constexpr. Auto inference does not imply dynamic typing; the inferred type is fixed by the declaration. Do not assume that every C++ auto/constexpr form is valid C23.

Allocated storage is the fourth storage-duration category. It is obtained through allocation facilities, not a storage-class keyword named “heap.” Its lifetime can end independently of the pointer variable that stores its address. The dynamic memory chapter develops ownership and release.

7. Thought check: does a long lifetime imply wide scope?

Can a variable survive a function call while its name remains local?

Yes: the static calls object does exactly that. Its lifetime lasts through program execution; its identifier is block-scoped with no linkage. Scope, linkage, duration, and initialization are related declaration properties, not synonyms.

Sources and further reading

Primary references: WG14 N1570 6.2.1, 6.2.2, 6.2.4, 6.5.3.2, 6.7.1, 6.7.6.3, 6.7.9, and 6.9.2; WG14 N3096 (C23 notes). These are public committee drafts; the examples here use a C17 baseline.

Before you move on

  • Scope, storage duration, and linkage describe different properties.
  • Objects with static storage duration persist for program execution; thread-duration objects are associated with a thread.
  • A storage-class keyword must be read in its declaration context rather than as a universal lifetime shortcut.
Keep these ideas separate
Scope / Linkage
Scope describes where a name is visible; linkage connects declarations that identify the same entity.
Explain it in your own words

How can a local static object outlive one execution of the block while its name stays block-scoped?

Optional: say it aloud or keep a note. You can continue whenever you are ready.

Check your understanding

Optional. Answers are not saved or sent, and reading stays unrestricted.

For a file-scope definition static int count;, which properties apply?
Read the explanation for every choice
  1. Static storage duration and internal linkage.

    ✓ Correct reasoning. These describe object duration and identifier linkage; the name is declared at file scope.

  2. Automatic duration limited to one function call.

    A file-scope object does not have ordinary automatic duration.

  3. A fresh dynamic allocation whenever the name is used.

    Referencing the name does not perform a new allocation.

C17

Ready for the next idea?

Mark this chapter as read whenever you feel ready.

CHAPTER 17 / 73

In this chapter

Storage classes: duration, scope, and linkage

Search all 73 chapters in both languages.EN + HG

Try “pointer”, “why int”, or “undefined behavior”.

Concepts connect. Follow your curiosity.↑ ↓ to explore · Enter to open
MAKE YOURSELF COMFORTABLE

Your reading space

Appearance

A clear canvas with balanced contrast.

Text size

100%
AA

Reading tools

Your data stays in this browser

Export a backup before changing browsers or replacing an older version of the reader.

YOUR PERSONAL MARGINS

My notebook

Saved on this device
A LITTLE CLARITY

The glossary

A starting meaning, then a chapter to go deeper.

A QUICK DEFINITION

A little clarity

Read the full explanation →

A thought worth keeping