Learn C
#include "gettingstarted/compilation_model.h"

The Compilation Model: Preprocess, Compile, Link in C

#concept

Building a C program is a three-stage pipeline behind the single gcc command. The preprocessor handles # lines (pasting headers, expanding macros, stripping comments), the compiler translates each .c file independently into object code, and the linker stitches objects and libraries into one executable.

Stage knowledge turns cryptic errors easy: syntax and type errors come from the compiler, while undefined reference is a linker error (declared but never defined or linked). Headers (.h) declare, sources (.c) define β€” splitting them is what makes multi-file projects work.

#how it works
1

Preprocessing

#include pastes headers in, #define expands macros β€” output is still C source, just bigger. Inspect it with gcc -E.

2

Compilation

Each .c file compiles separately to a .o object file. Syntax errors, type mismatches, and unknown names fail here.

3

Linking

The linker combines objects and libraries into the executable, resolving every call to a real definition.

4

Headers vs sources

Headers declare (signatures, struct layouts); .c files define (bodies). Including without defining causes linker β€” not compiler β€” errors.

#example
// math.h: int square(int x); (declaration)
// math.c: int square(int x) { return x * x; } (definition)
// main.c: #include "math.h" ... square(6) ...
// Build: gcc main.c math.c -o app -Wall
Output
36
1main.c includes only the declaration, so it compiles knowing square's signature without its body.
2math.c compiles separately into its own object with the real implementation.
3The linker joins both; omitting math.c would fail here with undefined reference to square.
#use cases
Diagnosing undefined reference as a link-time (not compile-time) problem
Splitting growing programs into headers plus .c files
Speeding rebuilds: only changed files recompile, then relink
Using -c (compile only) and -o (name output) in real build commands

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.