C Language
1. Getting Started with CBeginner10 min read

C Compilation Process & Memory Layout

Understand the four stages of transforming human-readable C code into native machine executables: Preprocessing, Compiling, Assembling, and Linking.

Key Takeaways & Core Concepts
  • 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:

  1. 1
    Preprocessing (`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).
  1. 2
    Compilation (`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).
  1. 3
    Assembly (`gcc -c`):
  • Converts human-readable assembly instructions into raw binary machine code.
  • Produces an object file (main.o or main.obj).
  1. 4
    Linking (`gcc -o`):
  • Resolves external symbols and function references (e.g., printf from libc).
  • Combines multiple object files and static/dynamic libraries.
  • Generates the final executable file (main.exe on Windows, main on Linux/macOS).
Inspect Intermediate Files with GCC

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 main executes.
  • 4. Heap Segment: Dynamically allocated memory requested at runtime via malloc(), calloc(), or realloc(). 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.
Demonstrating C Memory Segments
1#include <stdio.h>
2#include <stdlib.h>
3
4int global_initialized = 42; // Stored in Data Segment
5int global_uninitialized; // Stored in BSS Segment (initialized to 0)
6
7int main(void) {
8 int local_stack_var = 100; // Stored on the Stack
9 int *heap_var = (int*)malloc(sizeof(int)); // Pointer on stack, memory on Heap
10 *heap_var = 500;
11
12 printf("Code segment (main): %p\n", (void*)&main);
13 printf("Data segment: %p\n", (void*)&global_initialized);
14 printf("BSS segment: %p\n", (void*)&global_uninitialized);
15 printf("Heap segment: %p\n", (void*)heap_var);
16 printf("Stack segment: %p\n", (void*)&local_stack_var);
17
18 free(heap_var); // Prevent memory leak
19 return 0;
20}
Click code or "Edit Code" to edit
Interactive Code Block•Tab = 4 spaces•Ctrl + Enter = Run
20 lines
Note: Notice the address hierarchy: Code and Data live in low virtual memory, Heap grows upward, and Stack variables live high up and grow downward.
Practice & AssessmentC Programming Complete Course

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)

1

Which toolchain component is responsible for resolving function calls like printf() to the C Standard Library?

Beginner
2

Where are uninitialized global variables stored in a C program's memory layout?

Intermediate