Skip to content

Module 4 · Functions, Recursion and Storage Classes — topic 3 of 3

Storage Classes and Scope

auto, register, static and extern decide where a variable lives, how long it lasts and which parts of a program can see it. Learn block scope, file scope, lifetime and linkage.

Beginner 15 min read·Lesson 18 of 37 in C Programming

Learning objectives

  • Distinguish scope, lifetime and linkage
  • Use static locals to keep state between calls
  • Share globals across files with extern and hide them with static

Definition

A storage-class specifier (auto, register, static, extern, _Thread_local) determines an identifier's storage duration (automatic, static or thread), and, together with where it is declared, its linkage (none, internal or external).

Introduction

Every variable has three properties beyond its type. Scope: where in the source its name can be used. Lifetime: when it exists in memory. Linkage: whether the same name in another file refers to the same object.

Storage-class keywords change these properties. static in particular means two different things depending on where you use it — a frequent interview question.

Why it matters

Understanding lifetime explains bugs such as returning the address of a local variable. Understanding linkage is essential for multi-file programs and libraries.

Static locals are a clean way to count calls, cache results or generate IDs without global variables.

Detailed explanation

Block scope: a variable declared inside { } is visible only inside that block. File scope: a variable declared outside all functions is visible from the declaration to the end of the file.

`auto` (the default for locals): automatic storage — created when the block is entered, destroyed when it exits, not initialised automatically. Writing auto explicitly is legal but redundant in C (in C23, auto can also mean type inference).

`register`: a hint that the variable is used heavily. You cannot take its address. Modern compilers ignore the hint for optimisation purposes.

`static` on a local variable: static storage duration — created once, lives for the whole program, keeps its value between calls, and is zero-initialised if not initialised explicitly. Scope stays the block.

`static` on a global variable or function: internal linkage — the name is private to its source file. `extern`: declares a variable defined elsewhere (usually another file) without allocating it.

DeclarationScopeLifetimeLinkageDefault value
local int x;blockautomaticnoneindeterminate
local static int x;blockwhole programnone0
global int x;filewhole programexternal0
global static int x;filewhole programinternal0
extern int x;where declaredwhole programexternaldefined elsewhere

Syntax

C
int shared = 0; /* external linkage, visible to other files */
static int private_ = 0; /* internal linkage, this file only */
extern int defined_elsewhere;
void f(void) {
int local = 1; /* automatic */
static int calls = 0; /* keeps value between calls */
register int i; /* hint; no & allowed */
}
  • A static local's initialiser runs only once.
  • extern declarations usually go in header files; the single definition goes in one .c file.
  • Inner declarations hide (shadow) outer ones with the same name.

Code examples

A static local counter

C
#include <stdio.h>
int next_id(void) {
static int id = 100; /* initialised once */
return ++id;
}
int main(void) {
printf("%d\n", next_id());
printf("%d\n", next_id());
printf("%d\n", next_id());
return 0;
}
Output
101
102
103

Block scope and shadowing

C
#include <stdio.h>
int x = 1; /* file scope */
int main(void) {
printf("global x = %d\n", x);
int x = 2; /* hides the global */
{
int x = 3; /* hides the outer local */
printf("inner x = %d\n", x);
}
printf("local x = %d\n", x);
return 0;
}
Output
global x = 1
inner x = 3
local x = 2

More examples

Automatic vs static lifetime

C
#include <stdio.h>
void visit(void) {
int automatic = 0;
static int persistent = 0;
automatic++;
persistent++;
printf("automatic=%d persistent=%d\n", automatic, persistent);
}
static int helper_calls = 0; /* private to this file */
static int square(int n) { /* private helper function */
helper_calls++;
return n * n;
}
int main(void) {
for (int i = 0; i < 3; i++) visit();
int total = square(3) + square(4);
printf("total=%d after %d helper calls\n", total, helper_calls);
return 0;
}
Output
automatic=1 persistent=1
automatic=1 persistent=2
automatic=1 persistent=3
total=25 after 2 helper calls

Step-by-step explanation

  • automatic is created fresh on each call and always starts at 0, so it prints 1 every time.
  • persistent is created once; its value survives between calls and increases to 3.
  • static int helper_calls at file scope cannot be referenced from other .c files.
  • static int square is a private helper — another file could define its own square without a clash.

Expected output

Running “Automatic vs static lifetime” prints exactly:

TextOutput
automatic=1 persistent=1
automatic=1 persistent=2
automatic=1 persistent=3
total=25 after 2 helper calls

Practical applications

  • Libraries mark internal helpers static so they don't clash with user code.
  • Random number generators keep their seed in a static variable.
  • Configuration values are shared across files with extern declarations in a header.

Best practices

  • Minimise globals; prefer passing values as parameters.
  • Make every file-level function and variable static unless other files need it.
  • Use static locals for state that belongs to one function.

Common mistakes

Returning the address of a local

Avoid

int *make(void) {
int value = 42;
return &value;
}

Prefer

int *make(void) {
static int value = 42;
return &value;
}

value is destroyed when the function returns, leaving a dangling pointer. A static (or heap-allocated) object outlives the call.

Defining a global in a header

Avoid

/* config.h */
int debug_level = 0;

Prefer

/* config.h */
extern int debug_level;
/* config.c */
int debug_level = 0;

Including the header in two files creates two definitions and a linker error. Declare with extern in the header and define once.

Important notes

  • Static locals make a function non-reentrant: it is unsafe to call from multiple threads at once without synchronisation.
  • _Thread_local (C11) gives each thread its own copy of a static-duration variable.

Key takeaways

  • Scope = where a name is visible; lifetime = when the object exists; linkage = sharing across files.
  • Static locals persist between calls and are zero-initialised.
  • static at file scope makes a name private; extern refers to a definition elsewhere.

Try it yourself

  1. 1
    Write a function that prints “first call” only the first time it is called, using a static flag.
  2. 2
    Split a program into main.c and counter.c, sharing a counter variable with extern.

Finished reading? Mark it complete to update your dashboard.

Suggested practice

6 questions test this exact topic. Answers and explanations are revealed after you submit.

  • BeginnerWhat is the initial value of a global `int count;` with no initialiser?
  • ModerateWhat is printed?
  • ModerateWhat does `static` mean on a function defined at file scope?
Practise this topic