Storage Classes, Structures and Enums in C: Formula Sheet with Worked Revision

Revise C declarations through three separate axes, then test them with a two-call trace, a byte-layout calculation and an enum sequence.

KnowledgeGate Team

Exam prep & CS education

Updated 17 Nov 20266 min read
Storage Classes, Structures and Enums in C: Formula Sheet with Worked Revision

auto, register, static and extern are often memorised as one list, even though questions test scope, storage duration and linkage separately. Structures add layout and padding, while enums add implicit integer values. This CS Fundamentals for Exams & Placements course places these rules in the wider syllabus. The rules become concrete in three checks: outputs 3 13 6, 3 16 6, 0 7; a structure shrinking from 12 bytes to 8 bytes; and enum values 1, 2, 5, 6.

1. C storage and user-defined types: the revision map

Start with three questions. Scope asks where an identifier is visible. Storage duration asks how long its object exists. Linkage asks whether declarations in different scopes or translation units can denote the same entity. Use that order for visibility, lifetime or cross-file identity.

auto and register normally describe block objects with automatic storage duration. static can describe a persistent block object or give a file-scope name internal linkage. extern normally declares an entity defined elsewhere. A struct stores members simultaneously, a union overlays them, and an enum creates named integer constants and an enumerated type. C classifies typedef as a storage-class specifier, but it creates an alias, not object storage.

Separate declaration from definition. extern int total; without an initializer is only a declaration. int total; at file scope is a tentative definition. static int cache; is a file-scope definition with internal linkage and zero initialisation. Use the declaration-definition distinction before tracing any storage or type rule.

2. Storage classes in C: scope, duration, linkage and initial value

form

where it can appear

storage duration

linkage

initialisation and exam cue

auto int x;

block

automatic

none

Indeterminate if not initialised. Create it again on each block entry.

register int r;

block

automatic

none

Indeterminate if not initialised. Unary & cannot be applied to the declared object, even if memory is used.

static int calls;

block

static

none

Zero-initialised once before program execution, then retains its value between calls.

static int cache;

file

static

internal

Zero-initialised and confined to its translation unit.

extern int total;

scope follows the declaration

static for the denoted object

normally external

With no initializer, the declaration does not allocate a second object.

For a two-file check, let counter.c contain int counter = 5; and report.c contain extern int counter;. Both denote the same external-linkage object. Change it to 9 through either translation unit, and the other observes 9. A static int counter = 5; in counter.c has internal linkage, so it cannot satisfy the external declaration.

The earlier C Storage Classes, Structs and Enums: sizeof Questions keeps the canonical combined treatment of storage-class traps, structure padding and enum increments. This page compresses those rules into a cross-topic formula sheet with a two-call trace and ABI byte accounting. For the standalone concept walkthrough, see Storage Classes in C: Scope, Lifetime, Linkage & Traps, which owns the detailed storage-class progression.

3. Static versus automatic storage: a two-call worked C trace

Trace each call boundary:

c
#include <stdio.h>

int global_total;
static int file_only = 7;

void tick(void) {
    auto int a = 1;
    static int s = 10;
    register int r = 2;
    a += 2;
    s += 3;
    r += 4;
    printf("%d %d %d\n", a, s, r);
}

int main(void) {
    tick();
    tick();
    printf("%d %d\n", global_total, file_only);
    return 0;
}

Call 1 gives a: 1 -> 3, s: 10 -> 13, and r: 2 -> 6, printing 3 13 6. On call 2, fresh automatic objects reset a to 1 and r to 2, while s resumes at 13. The updates print 3 16 6.

The final line is 0 7: global_total is zero-initialised, while file_only starts at 7. Both file-scope objects and local s have static duration. Only file_only has internal linkage. Local s has no linkage, and global_total has external linkage. register does not guarantee hardware placement.

Diagram compares two calls to tick: static objects persist, automatic a and r reset, local static s advances, with outputs 3 13 6, 3 16 6, 0 7.

4. Structures in C: declaration, access, copying and byte layout

Use this record:

c
struct Record { char code; int count; short flag; };
struct Record r = {'A', 1000, 2};
struct Record *p = &r;
struct Record copy = r;

Use r.count for the object and p->count through the pointer. struct Record copy = r; copies the whole structure value. Members stay in declaration order, but an implementation may insert padding between members and after the last one.

Assume this illustrative ABI: char size/alignment 1/1, short 2/2, int 4/4, and structure alignment 4. In Record, code='A' is at byte 0; bytes 1 to 3 are padding; count=1000 uses bytes 4 to 7; flag=2 uses bytes 8 to 9; and bytes 10 to 11 are tail padding. Thus sizeof(struct Record) = 12, not 1 + 4 + 2 = 7.

For struct Better { int count; short flag; char code; };, count=1000 uses bytes 0 to 3, flag=2 bytes 4 to 5, code='A' byte 6, and tail padding byte 7. Under the same ABI, sizeof(struct Better) = 8.

For 100 elements, the layouts use 100 x 12 = 1200 and 100 x 8 = 800 bytes, a difference of 1200 - 800 = 400 bytes. These offsets are not universal. sizeof and offsetof on the target implementation decide the real layout. See C Structures: Declaration, Unions and Worked Examples. This page keeps the focus on ABI padding, byte accounting and how those calculations sit beside storage-class and enum rules.

Byte-layout diagram compares two C structs under a stated ABI: Record occupies 12 bytes with padding, Better occupies 8, saving 400 bytes across 100 records.

5. Enums, structures and unions: the quick comparison

Consider enum State { IDLE = 1, READY, RUNNING = 5, DONE };. IDLE is explicitly 1, so READY becomes 2. The explicit assignment then resets the sequence to RUNNING = 5, so DONE becomes 6. With enum State current = READY;, the comparison current == 2 is true in this constructed example, though current == READY is clearer.

construct

storage model

when to use it

struct

Every member has separate storage, and all member values coexist.

Use it for a record with simultaneous fields.

union

Members overlap in one shared region, holding one member representation at a time.

Use it for alternative representations when the active member is tracked.

enum

Named integer constants plus an enumerated type.

Use it for a closed, named state set.

Do not assume a universal sizeof(enum State) or one compatible integer type across every compiler. Also remember two syntax traps. In C, struct Record is the type name unless a separate alias such as typedef struct Record Record; is introduced. Enum values continue from the immediately preceding enumerator, not from the first value.

6. Storage classes, structures and enums: how questions test the rules

Practise tracing a static local, classifying scope, duration or linkage, deciding whether two files share an extern object, spotting an invalid address operation on register, computing offsets from stated ABI data, and filling enum values after explicit assignments.

trap

why it fails

correction

Static always means internal linkage.

A block static has no linkage.

Separate block and file static.

An uninitialised local is zero.

An automatic local has an indeterminate value.

Static-duration objects are zero-initialised by default.

extern creates a fresh object in every file.

It can denote an existing definition.

Separate declaration from definition.

Structure size is the sum of member sizes.

Alignment may add padding.

Draw aligned, numbered byte positions.

Enum values restart at zero after an explicit value.

The next value increments the previous enumerator.

Write every value in order.

register guarantees hardware placement.

Placement is the compiler's choice.

Remember only the language restrictions and automatic duration.

When you practise, classify each prompt by scope, duration or linkage; trace call boundaries, draw numbered bytes for the stated ABI, and write every enumerator value before choosing an answer.

7. Storage classes, structures and enums in the short version

Keep six retrieval lines: auto and register objects usually die at block exit. A local static persists without linkage. A file static has internal linkage. extern normally names a definition elsewhere. Structure layout follows alignment as well as member sizes. Enum implicit values increment from the previous enumerator.

For a ten-minute revision, reproduce 3 13 6, 3 16 6, 0 7; redraw the 12-byte and 8-byte layouts; then recover 1, 2, 5, 6 without looking back. For the broader subject sequence, GATE Guidance by Sanchit Sir is a structured GATE CS preparation route.