5.1.1 · HinglishC Programming

C compilation — preprocessor, compiler, assembler, linker

1,675 words8 min readRead in English

5.1.1 · Coding › C Programming


Compilation mein stages KYU hote hain?

CPU sirf raw bytes (opcodes) samajhta hai. C source mein human ke liye bahut saari cheezein hain: #include, macros, comments, main, printf jaisi names. Yeh gap bahut bada hai, isliye hum divide and conquer karte hain — har tool ek hi sub-problem solve karta hai:

  • Preprocessor → text substitution handle karta hai (#-directives).
  • Compiler → real C ko tumhare CPU ke liye assembly mein translate karta hai.
  • Assembler → assembly ko machine code mein convert karta hai (ek object file).
  • Linker → object files + libraries ko ek executable mein jod deta hai.

Figure — C compilation — preprocessor, compiler, assembler, linker

Stage 1 — Preprocessor (cpp)

YEH KYA KARTA HAI:

  • #include <stdio.h> → us header file ka literally poora content wahan paste kar deta hai.
  • #define PI 3.14macro: har PI token ko 3.14 se replace karta hai.
  • #ifdef / #ifndef / #endif → conditional compilation (code tab hi include karo jab ek macro defined ho).
  • Saare comments strip kar deta hai.

DEKHNE KA TARIKA:

gcc -E hello.c          # output preprocessing ke baad ruk jaata hai

Stage 2 — Compiler (cc1)

YEH KYA PRODUCE KARTA HAI: ek .s text file jisme human-readable assembly mnemonics hain (mov, add, call).

DEKHNE KA TARIKA:

gcc -S hello.c          # hello.s produce karta hai

Stage 3 — Assembler (as)

OBJECT FILE MEIN KYA HOTA HAI:

  • Tumhare functions ka machine code.
  • Ek symbol table jo yahan defined names (jaise main) aur woh names jo chahiye hain lekin abhi nahi mile (jaise printf) — undefined mark karke — list karta hai.

DEKHNE KA TARIKA:

gcc -c hello.c          # hello.o produce karta hai (abhi runnable nahi!)

Stage 4 — Linker (ld)

YEH KYA SOLVE KARTA HAI:

  • Har undefined symbol (printf) ko libc mein uski defined location se match karta hai.
  • Saare code/data sections merge karta hai aur final memory addresses assign karta hai.
  • Startup code (_start) add karta hai jo main ko call karta hai.

Forecast-then-Verify

Recall Padhne se pehle har flag ka output predict karo

gcc -E ::: preprocessed source (.i) gcc -S ::: assembly (.s) gcc -c ::: object file (.o), no linking gcc (koi flag nahi) ::: full executable Har jagah ruka? Ek chhoti file par run karke verify karo.


Common Mistakes (Steel-manned)


Mnemonic


Feynman: ek 12-saal ke bacche ko explain karo

Recall Click to read

Socho tum ek robot chef ko recipe de rahe ho jo sirf button-presses samajhta hai.

  1. Preprocessor = tumhara assistant jo koi bhi borrowed recipe pages paste karta hai jinka tumne reference diya tha ("cake recipe dekho") aur shorthand jo tumne define ki thi copy karta hai.
  2. Compiler = tumhari English recipe ko robot ki step-by-step instruction language mein translate karta hai.
  3. Assembler = un instructions ko exact button-press codes mein convert karta hai jo robot padhta hai.
  4. Linker = agar tumhari recipe kehti hai "Grandma's frosting use karo," toh yeh jaata hai Grandma ke frosting steps lata hai aur staple karta hai, taaki robot ke paas ek complete booklet mein sab kuch ho jo woh start to finish run kar sake.

Active-Recall Flashcards

What are the 4 stages of C compilation in order?
Preprocessor → Compiler → Assembler → Linker
Which stage handles #include and #define?
The preprocessor (text substitution, before real compilation)
What does the compiler output?
Assembly language (.s), specific to the target CPU
What does the assembler output and what's special about it?
An object file (.o) with machine code + a symbol table containing undefined references
Why can't you run a .o file directly?
It has unresolved symbols (e.g. printf) and no entry-point setup; the linker must resolve them first
Which stage produces an "undefined reference" error?
The linker — a needed symbol's definition was never found
Do header files contain function code?
No — only declarations/prototypes; the actual code is in libraries, pulled in by the linker
gcc flag to stop after preprocessing?
gcc -E
gcc flag to stop after compiling (get assembly)?
gcc -S
gcc flag to stop after assembling (get .o, no link)?
gcc -c
Why wrap macro parameters in parentheses?
Macros are dumb text substitution; without parens, operator precedence breaks the intended math
What is relocation (linker)?
Assigning final memory addresses and patching references across merged object files
What's the file trail of the pipeline?
.c → .i → .s → .o → executable

Connections

Concept Map

motivates staging

input to

expands #-directives

translates C to

input to

encodes into

input to

linked into

stitches into

C source hello.c

CPU understands only bytes

Preprocessor cpp

Compiler cc1

Assembler as

Linker ld

Assembly .s

Object file .o

Libraries e.g. printf

Executable binary