C
Storage Classes in C
Storage Classes in C

Where can a variable be used, and how long does it exist?

Storage classes help describe scope, storage duration, linkage and initialization behavior.

Which variable should remember its value between function calls?
Connect this topic to the earlier variables lecture. Ask what happens to an ordinary local variable after a function returns.
counter.cSame function, different behavior
void show(void) { static int count = 0; count++; printf("%d\n", count); }
Core idea

What does a storage class tell us?

Scope

Where is the name visible?

Inside a block, function or source file.

Lifetime

How long does it exist?

For one block execution or for the whole program.

Linkage

Can another file share it?

Important for file-scope static and extern.

Initialization

What if no value is given?

Automatic locals are indeterminate; static-duration objects start at zero.

Four specifiers: auto - ordinary local   |   register - optimization hint   |   static - persistent storage   |   extern - defined elsewhere
Keep the first explanation simple: scope means where the name is usable; lifetime means how long the object exists. Introduce linkage only as sharing or restricting names across source files.
Automatic local variables

The default behavior inside a block

auto_demo.c
void show(void) { int count = 0; count++; printf("%d\n", count); }

Main properties

The keyword auto is normally omitted because ordinary block-scope variables are automatic by default.
  • Block scope
  • Automatic storage duration
  • Created when execution enters the block
  • Stops existing when execution leaves the block
  • Uninitialized value is indeterminate
Do not say that automatic variables are always stored on a stack; that is an implementation detail.
Interactive comparison

Automatic variable versus static local variable

automatic_counter.c
Automatic count after current call
0
No calls yet
Each automatic variable is created again for a new function call.
Ask students to predict the output before each click. Automatic mode should produce 1, 1, 1; static mode should produce 1, 2, 3.
Scope and lifetime

A static local remains alive but stays local

Inside show()

count is visible here.

Inside main()

The count declared inside show() cannot be accessed by name from main().

Lifetime across program stages

Static storage duration: the object exists throughout program execution and retains its value between function calls.
Block scope: its name can be used only inside the block where it is declared.
Initialization occurs only once. If no explicit initializer is provided, a static object is initialized to zero.
Two uses of static

Local static and file-scope static are not the same idea

DeclarationScopeStorage durationMain effect
static int count;
inside a function
BlockEntire programRetains value between calls
static int total;
outside functions
FileEntire programInternal linkage: name is limited to that source file

Local static

void show(void) { static int count; }

File-scope static

static int total = 100; int main(void) { /* total is available here */ }
register

A request, not a guarantee

register_demo.c
void process(void) { register int i; for (i = 0; i < 10; i++) { /* repeated use of i */ } }

Important facts

  • Automatic storage duration
  • Block scope
  • Compiler may ignore the hint
  • Modern compilers choose optimizations themselves
  • The address-of operator cannot be applied to a register object
Do not claim that every register variable is physically stored in a CPU register.
extern

Declare here, define elsewhere

data.c
int total = 100; /* Definition: storage is provided here. */
main.c
#include <stdio.h> extern int total; int main(void) { printf("%d", total); return 0; }
Shared total: 100
main.c reads total as 100.
extern int total; normally declares the same object; it does not create a second independent variable. Keep compilation and linking details for a later lecture.
Comparison

Storage classes at a glance

Initialization: an uninitialized automatic local has an indeterminate value; an object with static storage duration is initialized to zero.
FormWhere usedLifetimeMain idea
autoInside a blockCurrent block executionDefault ordinary local variable
registerInside a blockCurrent block executionCompiler hint; address cannot be taken
local staticInside a blockEntire programRetains value between calls
file staticOutside functionsEntire programName restricted to that source file
externUsually file scopeRefers to a static-duration objectObject is defined elsewhere
The table is the exam-oriented summary. Avoid overloading students with formal linkage categories beyond internal linkage for file-scope static and external sharing through extern.
Storage-class matching

Select the most suitable storage class

A function-call counter must preserve its value between calls.

Question 1 of 4

Result

Choose an answer.
Common misconceptions

Reveal the correction

“A register variable is guaranteed to be stored in a CPU register.”
False. register is only a hint, and the compiler may ignore it.
“A static local variable can be accessed from every function.”
False. It exists throughout program execution, but its name remains limited to its block.
“extern creates a new separate variable.”
False. An extern declaration normally refers to an object defined elsewhere.
The fourth initialization misconception is already covered on the comparison slide, so it is omitted here to keep the activity short.
Quick assessment

Check your understanding

Question 1 of 7
Final recap

Remember the purpose of each storage class

auto

Ordinary local

Exists during execution of its block.

register

Optimization hint

Automatic local whose address cannot be taken.

static

Persistent storage

Retains value or limits file-scope linkage.

extern

Defined elsewhere

Declares an object or function provided elsewhere.

Scope tells where a name is visible. Storage duration tells how long the object exists.