Storage Classes: auto, static, extern, register in C
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.
auto: ordinary locals
Block variables are automatic by default β created on entry, destroyed on exit. The auto keyword adds nothing and is never used.
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).
extern: declare elsewhere-defined
extern int total; references a global defined in another file β declaration here, single definition there, linker joins them.
register: historical hint
register int i; suggested register storage; modern compilers ignore it (and forbid & on such variables) β know it for reading old code.
#include <stdio.h>Β void counter(void) { static int calls = 0; ++calls; printf("call %d\n", calls);}Β int main(void) { counter(); counter(); return 0;}Recommended resourcesAffiliate links Β· may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in C yourself β building beats watching.