Skip to main content

Compilation, linking and loading process

Short answer

A C program goes through preprocessing, compilation (source to object code), linking (object code combined with libraries into an executable), and loading (copying the executable into RAM to run). For example, hello.c becomes hello.o, then hello.exe, and loading displays Hello.

Explain the program compilation, linking and loading process with example.

After it is written (edited), a C program goes through four stages before it runs: preprocessing, compiling, linking and loading.

 Source  --> Preprocessor --> Compiler --> Object
 (prog.c)    (#include,                    (prog.o)
              #define)                        |
                                             v
 Running <-- Loader <-- Executable <-- Linker <-- Libraries
 process     (RAM)      (prog.exe)               (stdio, math)
  1. Preprocessing: directives such as #include <stdio.h> and #define are processed.
  2. Compilation: the compiler checks syntax and converts the source into machine-level object code. Syntax errors are reported here.
  3. Linking: the linker joins the object code with library code (for example, the code of printf) and creates the executable file.
  4. Loading: the loader copies the executable from disk into main memory and starts execution at main().

Example: for hello.c containing printf("Hello");, compiling gives hello.o, linking with the C library gives hello.exe, and running it loads the file into RAM and displays Hello.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Computer Programming chapter 1 questions →