Storage-class specifiers help describe how declarations relate to storage duration and linkage. Their scope depends on where the declaration appears.
Auto
A block-local auto object has automatic storage duration and no linkage. It is normally created for an execution of its block. Without a valid initialisation, an ordinary automatic scalar does not have a useful guaranteed starting value.
Register
A block-local register object is also automatic and has no linkage. It suggests frequent access, but the compiler does not have to place it in a hardware register. In C11, its address cannot be taken with &.
Static
A local static object has block scope, no linkage and a lifetime covering program execution. It retains its value across calls. A file-scope static object instead has file scope and internal linkage. Arithmetic objects with static storage duration are initialised to zero if no explicit initialiser is given.
Extern
A common extern declaration refers to an object defined elsewhere. That object can be shared across source files through compatible external-linkage declarations. A declaration without an initialiser does not ordinarily create a separate new object.
Example and important distinction
Use an automatic temporary for a calculation needed only during one call. Use a local static counter when lasting state is intentional. Use an extern declaration when referring to a shared definition. Do not choose a class solely by a simplified memory-location table: C does not require automatic objects to occupy a particular physical stack or register. State scope, duration and linkage separately.
Comparison table
↔ Scroll horizontally on a small screen
Storage classes in C11
Declaration
Scope
Duration
Linkage / main rule
Block auto
Block
Automatic
None; initialise before reading
Block register
Block
Automatic
None; address cannot be taken in C11
Block static
Block
Static
None; retains value
File static
File
Static
Internal
Typical extern declaration
At declaration scope
Static for referred external object
Refers to compatible shared definition
Related exam questions
Related questions in the storage classes category from the supplied papers. The marks below are the printed allocations, including any combined subquestion.
September/October 2021 · Q1(b) · 8 marks
If a variable has auto storage class by default, why was the auto keyword introduced in C? Explain.