Learn C
#include "variablesdatatypes/storage_classes.h"

Storage Classes: auto, static, extern, register in C

#concept

Storage classes control a variable's lifetime and visibility. auto is the default for locals (automatic storage, dies at block exit β€” the keyword itself is nearly never written). static stretches lifetime to the whole program while keeping scope local: function-static variables remember values between calls, and file-static globals stay invisible to other files.

extern declares without defining (referencing a global defined elsewhere), and register merely suggests CPU-register storage to the compiler (largely ignored on modern optimizers, and you can't take such a variable's address). Mastering static vs extern is mastering multi-file C programs.

#how it works
1

auto: ordinary locals

Block variables are automatic by default β€” created on entry, destroyed on exit. The auto keyword adds nothing and is never used.

2

static: persistent but scoped

static int calls = 0; inside a function survives between calls; at file scope it hides the name from other files (internal linkage).

3

extern: declare elsewhere-defined

extern int total; references a global defined in another file β€” declaration here, single definition there, linker joins them.

4

register: historical hint

register int i; suggested register storage; modern compilers ignore it (and forbid & on such variables) β€” know it for reading old code.

#example
#include <stdio.h>
Β 
void counter(void) {
static int calls = 0;
++calls;
printf("call %d\n", calls);
}
Β 
int main(void) {
counter();
counter();
return 0;
}
Output
call 1
call 2
1calls initializes once at program start β€” not on each entry β€” because static extends its lifetime.
2Each call increments the same persisting variable, printing 1 then 2.
3Despite persisting, calls remains scoped inside counter β€” no other function can name it.
#use cases
Call counters and one-time-init flags with function-static variables
Hiding file-private helpers and state with file-scope static
Sharing globals across files via one definition plus extern declarations
Recognizing register in legacy code without writing it yourself

Recommended resourcesAffiliate links Β· may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice β€” pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from β€” great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in C yourself β€” building beats watching.