Skip to study material
← Question index

QUESTION 28 · Storage Classes

Compare auto, register, static and extern in C11.

5 marks

Answer in brief

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
DeclarationScopeDurationLinkage / main rule
Block autoBlockAutomaticNone; initialise before reading
Block registerBlockAutomaticNone; address cannot be taken in C11
Block staticBlockStaticNone; retains value
File staticFileStaticInternal
Typical extern declarationAt declaration scopeStatic for referred external objectRefers 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.

    Paper P01 · page 1
  • July 2021 · Q2(c) · 5 marks

    List and explain storage classes in C.

    Paper P04 · page 1

View this category in the exam question bank →