C Compilation Process & Memory Layout
Understand the four stages of transforming human-readable C code into native machine executables: Preprocessing, Compiling, Assembling, and Linking.
- A C source file (.c) undergoes 4 distinct phases before turning into a runnable executable (.exe or ELF binary).
- Preprocessor expands macros, strips comments, and copies header files (.h).
- Compiler translates preprocessed C into assembly instructions (.s).
- Assembler converts assembly code into relocatable machine object code (.o or .obj).
- Linker bundles object files with standard library definitions into the final executable.
- At runtime, memory is divided into Text/Code segment, Data segment, BSS, Stack, and Heap.
The 4 Phases of C Compilation
When you invoke a compiler like GCC (gcc main.c -o main), the toolchain executes four transparent phases:
- 1Preprocessing (`gcc -E`):
- Strips all source code comments.
- Evaluates preprocessor directives beginning with
#(e.g.#include,#define,#ifdef). - Copies the full contents of header files into the output stream.
- Produces an expanded intermediate file (
main.i).
- 2Compilation (`gcc -S`):
- Analyzes syntax, checks types, performs compiler optimizations.
- Translates high-level C logic into assembly code specific to the host CPU architecture (
x86_64,ARM, etc.). - Outputs an assembly file (
main.s).
- 3Assembly (`gcc -c`):
- Converts human-readable assembly instructions into raw binary machine code.
- Produces an object file (
main.oormain.obj).
- 4Linking (`gcc -o`):
- Resolves external symbols and function references (e.g.,
printffrom libc). - Combines multiple object files and static/dynamic libraries.
- Generates the final executable file (
main.exeon Windows,mainon Linux/macOS).
Run `gcc -save-temps main.c -o main` in your terminal to inspect main.i (preprocessed), main.s (assembly), and main.o (object binary) right in your workspace!
Memory Layout of a C Program in RAM
When the operating system's loader executes a compiled C binary, it arranges the virtual address space into five distinct segments:
- 1. Text / Code Segment: Read-only segment storing compiled machine instructions.
- 2. Initialized Data Segment (Data): Stores global, static, and constant variables initialized with non-zero values (e.g.,
int counter = 10;). - 3. Uninitialized Data Segment (BSS): Stores uninitialized global and static variables (Block Started by Symbol). Automatically zeroed out by the OS loader before
mainexecutes. - 4. Heap Segment: Dynamically allocated memory requested at runtime via
malloc(),calloc(), orrealloc(). Grows upwards towards higher memory addresses. Managed manually by the programmer. - 5. Stack Segment: Stores local function variables, parameter arguments, and return addresses. Grows downwards towards lower memory addresses. Follows Last-In-First-Out (LIFO) discipline.
Test Your Understanding
Reinforce core concepts with interactive multiple-choice questions, live coding challenges, and top technical interview questions.
Your Current Score
0 / 2 correct(0 attempted)
Which toolchain component is responsible for resolving function calls like printf() to the C Standard Library?
BeginnerWhere are uninitialized global variables stored in a C program's memory layout?
Intermediate