AdvancedC Programming · Lesson 11 of 15 · Updated ·
Storage Classes in C — auto, static, extern, register, Scope, Lifetime and Linkage
When you declare a variable in C, the compiler has to answer three questions about it. A storage class (auto, static, extern, register) is the keyword that changes the answers. These questions appear in almost every C viva and in "predict the output" rounds of placement tests.
1. Three questions: scope, lifetime, linkage
- Scope — where in the code can you use the name? A variable declared inside
{ }has block scope: it can be used only inside that block. A variable declared outside all functions has file scope: it can be used anywhere below its declaration in that file. - Lifetime (also called storage duration) — how long does the memory exist while the program runs? Automatic variables are created when the block starts and destroyed when it ends. Static variables are created once, before
mainstarts, and live until the program ends. - Linkage — can another
.cfile refer to this name? External linkage: yes. Internal linkage: only this file. No linkage: it is a local name.
Scope is about the name; lifetime is about the memory. They are not the same thing. A static local variable has block scope (its name works only inside the function) but static lifetime (its value survives between calls).
2. auto — the normal local variable
Every local variable is auto unless you say otherwise, so nobody writes the keyword. An auto variable:
- lives on the stack,
- is created fresh every time its block runs,
- holds garbage (an unpredictable value) until you assign it.
Reading an uninitialised auto variable is undefined behaviour, so always give locals a starting value.
#include <stdio.h>
int main(void) {
for (int i = 1; i <= 3; i++) {
int total = 0; /* created fresh on every loop pass */
total += i * 10;
printf("pass %d: total = %d\n", i, total);
}
return 0;
}
pass 1: total = 10
pass 2: total = 20
pass 3: total = 30
total never reaches 60, because it is a new variable, set to 0, on every pass.
3. static local — keeps its value between calls
Add static to a local variable and its lifetime becomes the whole program. It is initialised only once, before the program starts — not every time the function is called. If you do not initialise it, it starts at 0.
Think of a ticket counter at a railway station. Each new passenger should get the next token number, not token 1 again.
#include <stdio.h>
int next_token(void) {
static int token = 100; /* set once, remembered between calls */
token++;
return token;
}
int main(void) {
printf("%d\n", next_token());
printf("%d\n", next_token());
printf("%d\n", next_token());
return 0;
}
101
102
103
The initialiser of a static variable must be a constant expression — static int token = 100; is fine, but static int t = n * 2; (where n is a parameter) does not compile.
Static locals are also the safe way to return an array from a function: a static array is not destroyed when the function returns.
#include <stdio.h>
const char *grade_name(int marks) {
static char buf[20]; /* survives after the function returns */
sprintf(buf, "%s", marks >= 40 ? "PASS" : "FAIL");
return buf;
}
int main(void) {
printf("%s\n", grade_name(72));
printf("%s\n", grade_name(35));
return 0;
}
PASS
FAIL
There is a catch: every call reuses the same buffer, so the second call overwrites the first answer. That is why such functions are called "not re-entrant".
4. Global variables, extern and linkage
A variable declared outside all functions is a global. It has static lifetime, starts at 0 if not initialised, and has external linkage — another file can use it.
To use a global from another file you write an extern declaration. extern says "this variable exists; its memory is defined somewhere else". It does not create the variable.
/* marks.c */
int total_students = 60; /* definition: memory is created here */
/* report.c */
extern int total_students; /* declaration: use the one in marks.c */
The rule is: one definition, any number of declarations. Usually the extern line goes in a header file (marks.h) that every file includes.
You can also use extern inside one file, to use a global that is defined further down:
#include <stdio.h>
int main(void) {
extern int seats; /* defined below main */
printf("seats = %d\n", seats);
seats -= 5;
printf("seats = %d\n", seats);
return 0;
}
int seats = 72;
seats = 72
seats = 67
5. static at file level — "private to this file"
static has a second meaning when used outside a function. It changes the linkage from external to internal: the global (or function) can be used only inside its own .c file.
/* bank.c */
static double balance = 0; /* other files cannot touch this */
static void log_entry(double amt) { } /* helper, hidden from other files */
void deposit(double amt) { balance += amt; log_entry(amt); }
This is how C programmers hide helpers and data — the C version of private. Two files can each have their own static int count; without a clash.
Where static is used | What it changes |
|---|---|
| inside a function | lifetime → whole program |
| outside functions (global or function) | linkage → this file only |
6. register — a hint for speed
register int i; asks the compiler to keep i in a CPU register. Modern compilers decide register use on their own, so the keyword is only a hint and is usually ignored. One rule still applies: you cannot take the address of a register variable — &i is a compile error.
7. Shadowing: the inner name wins
If a block declares a name that already exists outside, the inner one hides (shadows) the outer one until the block ends.
#include <stdio.h>
int x = 1; /* global */
int main(void) {
printf("%d\n", x); /* global x */
int x = 2; /* local x hides the global */
{
int x = 3; /* hides the local x */
printf("%d\n", x);
}
printf("%d\n", x); /* back to the local x */
return 0;
}
1
3
2
Shadowing is legal but confusing. Use different names in real code.
Common mistakes
- Expecting a normal local variable to remember its value between calls — only
staticlocals do. - Thinking
static int c = 0;inside a function resetscto 0 on every call. It is initialised once. - Writing
extern int x = 5;in a header — with an initialiser it becomes a definition, and including it in two files gives a "multiple definition" link error. - Defining the same global (without
static) in two.cfiles. - Taking the address of a
registervariable. - Returning the address of an
autolocal — it is destroyed when the function returns (a dangling pointer).
Quick recap
- Scope = where the name works; lifetime = how long the memory lives; linkage = which files can see it.
auto: stack, garbage until set, created on each entry to the block.staticlocal: one copy for the whole run, initialised once, default 0.- Globals live for the whole program, default 0, and are visible to other files through
extern. staticon a global or function makes it private to its file.registeris only a hint; you cannot use&on it.