A storage class specifier controls lifetime, linkage, and where an object may live. C has auto, register, static, extern, and (C11) _Thread_local. Only one storage class may appear in a declaration.
Remember
auto int a; /* default for locals */
register int i; /* hint: keep in a register */
static int s; /* persists / file-private */
extern int g; /* defined in another file */
Reference
Cheat Sheet
Keep these facts handy while you work with Storage Classes in C.
Topic
Detail
auto
Default for locals; rarely written
register
Hint; cannot take its address
static
Persistent local or file-private name
extern
Refers to an object defined elsewhere
Code
C Examples
Compile each example with gcc file.c -std=c11 -Wall -o out. Next: auto and register.
Example 1 — Static, extern, and automatic together
A file-private variable and an extern view of a shared one.
C
#include <stdio.h>
int shared = 10; /* external linkage */
static int hidden = 20; /* internal linkage */
void demo(void) {
extern int shared; /* refers to the global above */
int local = 1; /* auto */
shared++;
printf("shared=%d hidden=%d local=%d\n", shared, hidden, local);
}
int main(void) {
demo();
demo();
return 0;
}