CS Atlas GATE 2027
Compiler Design course
0 / 90 complete

Compiler Design/Phase 0 · Foundations

Lesson 0.1.2 · Item 3 of 90

The toolchain: preprocessor, compiler, assembler, linker, loader

Read at your own pace. Try the questions before opening the answers.

On this page

0 · Recall Warm-up

Answer from memory first. Answers are in the Answer Vault at the end of this lesson.

  • What is the difference between compile time and run time? from Lesson 0.1.1
  • Why can’t the processor run the line total = price * qty + 50; directly? from Lesson 0.1.1
  • Does a compiled program need the compiler present when it runs? from Lesson 0.1.1

1 · The Story

📖

How Mr. Patel’s booklet is really made

Mr. Patel chose the booklet. Mrs. Desai shows him the assembly line it passes through.

1. Nisha, the clerk. Mr. Patel’s text contains notes like [insert: standard greeting page] and [GREETING means “Namaste and welcome”]. Nisha replaces each note with the full text it stands for. She does not translate anything and she does not think about meaning: she copies text exactly.

2. The translation team. They translate the expanded speech into English, but they write it in the typesetters’ shorthand: short codes that are still readable by people, one code per printing step.

3. Zoya, the typesetter. Zoya turns the shorthand into metal printing plates, one set per chapter. Page numbers on her plates start from 1 in every chapter, and some plates say “see the Thank-You page”, a page that is not in this chapter at all: it lives in the House’s library of standard pages.

4. Kiran, the binder. Kiran collects the plates of all chapters plus the needed library pages, renumbers every page into one sequence, and fills in every “see page …” with the real page number. Now there is one complete booklet.

5. Arjun, the usher. On the evening, Arjun places the booklets on the seats and signals “start”.

2 · Predict First

🤔

If Mr. Patel’s speech says “see the Thank-You page” but nobody ever wrote that page, which worker on the line is the first to notice? Write your guess down. The answer is in the vault.

3 · Core Concept

The five tools in order

When you type gcc hello.c, five tools run one after another. Each one reads the previous tool’s output.

1. Preprocessor

The preprocessor deals with the lines that start with #:

  • #include <stdio.h> pastes the whole header file stdio.h (declarations of library functions such as printf) into the program.
  • #define N 10 defines a macro: every later N is replaced by the text 10.
  • In real C toolchains it also replaces every comment by a space.

The replacement is purely textual: the preprocessor does not understand C.

2. Compiler

The compiler translates the expanded C program into assembly language: machine instructions written as readable short words (mnemonics) such as MOV, ADD, MUL, one instruction per line. This is the part of the toolchain the rest of these notes are about.

3. Assembler

The assembler converts assembly language into machine code. The result is an object file (hello.o). It is relocatable: its addresses are counted from 0 as if it were alone in memory, and calls to functions defined elsewhere, such as printf, are left as unresolved references.

4. Linker

The linker joins several object files and the needed library code (for example the C standard library that contains printf), resolves every external reference to a real address, and adjusts addresses so that all pieces fit together. The result is one executable file (a.out or hello.exe).

If a function is used but defined nowhere, the linker reports undefined reference.

5. Loader

The loader is part of the operating system. When you run the program, it copies the executable from disk into memory, sets up the program’s starting state, adjusts addresses if the program lands at a different memory location, and jumps to its first instruction.

File names along the way

StageToolOutput file (gcc)gcc option to stop here
expand # linespreprocessorhello.igcc -E
translatecompilerhello.sgcc -S
encodeassemblerhello.ogcc -c
joinlinkera.out(default)
runloader (OS)program in memory./a.out

Story → compiler term

In the storyIn the compiler
Nisha replacing notes with full textpreprocessor (#include, #define)
[insert: standard greeting page]#include of a header file
[GREETING means “Namaste and welcome”]#define macro
the translators’ shorthandassembly language
the translation teamcompiler
Zoya’s plates, numbered from 1 per chapterrelocatable object file from the assembler
“see the Thank-You page”reference to a function defined elsewhere (e.g. printf)
the House library of standard pageslibrary (e.g. the C standard library)
Kiran joining chapters and fixing page numberslinker
the complete bookletexecutable file
Arjun placing booklets on seats and saying “start”loader

4 · Visual

Diagram: hello.c · source program · Preprocessor · expands # lines · hello.i · Compiler · C → assembly · hello.s · Assembler · assembly → machine code · hello.o · Linker · + library code · a.out · Loader · disk → memory, start

The toolchain behind one gcc command. Each arrow carries a file to the next tool.

5 · Worked Examples

Example 1 Easy

Put these tools in the order they act on hello.c: linker, compiler, loader, preprocessor, assembler.

Solution

preprocessor → compiler → assembler → linker → loader. Same as the booklet line: clerk, translators, typesetter, binder, usher.

Example 2 Medium

Given #define SQUARE(x) x*x, what value does SQUARE(2+3) produce?

Solution

The preprocessor replaces text exactly, like Nisha copying notes without thinking: SQUARE(2+3) becomes 2+3*2+3.

Multiplication happens first: 2 + 6 + 3 = 11, not 25. The fix is to write #define SQUARE(x) ((x)*(x)).

Example 3 GATE

Three faulty programs: (i) #include <stdoi.h> (misspelled header); (ii) a missing semicolon after a statement; (iii) prinft("hi"); (misspelled function name; there is no function prinft anywhere). Which tool reports each problem first?

Solution

(i) Preprocessor: it cannot find the file stdoi.h to paste.

(ii) Compiler: the C text is not well formed.

(iii) The compiler only warns that prinft is not declared; it still emits a call. The linker fails with “undefined reference to prinft”, exactly like Kiran finding no Thank-You page.

6 · Shortcuts

⚡

.c → .i → .s → .o → a.out: preprocess, compile, assemble, link. Mnemonic: “I See One App”. Valid when: gcc-style Unix toolchains; Windows uses .obj and .exe for the last two.

⚡

Who complains? # line problems → preprocessor; badly formed C → compiler; a used function defined nowhere → linker; cannot place in memory or start → loader. Valid when: error-classification questions about the toolchain.

7 · GATE Traps

⚠️

  • Macro expansion is textual: no evaluation and no added parentheses (SQUARE(2+3) is 11).
  • The assembler’s output is not executable yet: addresses are relative and external references are unresolved.
  • An “undefined reference” is a linker error, not a compiler error.
  • The loader belongs to the operating system, not to the compiler.
  • In everyday speech “the compiler” (the gcc command) runs the whole toolchain; in exam questions “compiler” means only the translation step.

8 · Memory Hook

🧠

Clerk, Translator, Typesetter, Binder, Usher = preprocessor, compiler, assembler, linker, loader. “Please Compile And Launch Later.”

9 · Connect the Dots

The compiler is one tool in a line of five. Next question: inside the compiler itself, the translation is done in several departments. What does each department receive, what does it hand on, and which mistakes does each one catch?

10 · Self-Test

GATE format. Try every question before opening the vault.

Q1MCQ1 markLinker

Which tool resolves calls to library functions such as printf into real addresses?

  • (A) preprocessor
  • (B) compiler
  • (C) assembler
  • (D) linker

Q2MCQ1 markAssembler

The output of the assembler is:

  • (A) a relocatable object file
  • (B) an executable loaded in memory
  • (C) preprocessed C source
  • (D) a list of compile errors

Q3NAT1 markMacro

With #define MUL(a,b) a*b, what is the value of MUL(2+1, 3+1)?

Numerical answer: ________

Q4MSQ1 markPreprocessor

Which of the following does the C preprocessor do?

  • (A) Paste the contents of header files named in #include
  • (B) Replace macros defined with #define
  • (C) Detect type mismatches such as adding an array to a number
  • (D) Run before the compiler

Q5MCQ1 markLoader

The loader:

  • (A) copies the executable into memory and starts it
  • (B) translates C into assembly
  • (C) joins object files
  • (D) expands macros

11 · Rule Card

  • Order: preprocessor → compiler → assembler → linker → loader.
  • Files: .c → .i → .s → .o → executable.
  • Preprocessor: #include, #define, textual only.
  • Assembler output: relocatable object code with unresolved external references.
  • Linker: joins objects + libraries, resolves references → executable; “undefined reference” errors.
  • Loader (OS): copies into memory, relocates if needed, starts execution.

12 · Concept Ledger

preprocessorheader filemacroassembly languageassemblerobject file (relocatable)librarylinkerexecutable fileloader

13 · Answer Vault

Answer Vault

Try the questions first. Open when you are ready to check.

Warm-up

  • Compile time is when the compiler translates the source program; run time is when the resulting target program executes on its inputs.
  • The processor executes only machine code (bit patterns for tiny steps); the C line is high-level text that must be translated first.
  • No. Once the target program exists, it runs on its own.

Predict First

Kiran, the binder. Nisha copies notes, the translators and Zoya work one chapter at a time and accept references to pages elsewhere; only the binder, who joins everything, finds that the page does not exist. In a real toolchain this is the linker, reporting an “undefined reference”.

Self-Test

Q1 Answer: (D)1 mark

The linker joins object files and library code and resolves external references.

  • (A) is wrong: It only expands # lines and macros.
  • (B) is wrong: It translates C into assembly and leaves external calls symbolic.
  • (C) is wrong: It produces relocatable object code with unresolved references.

Re-read: Lesson 0.1.2 — The toolchain: preprocessor, compiler, assembler, linker, loader

Q2 Answer: (A)1 mark

The assembler encodes assembly as machine code with relative addresses: a relocatable object file.

  • (B) is wrong: That is the loader’s result after linking.
  • (C) is wrong: That is the preprocessor’s output.
  • (D) is wrong: Errors are a side report, not the main output.

Re-read: Lesson 0.1.2 — The toolchain: preprocessor, compiler, assembler, linker, loader

Q3 Answer: 61 mark

Textual expansion gives 2+1*3+1 = 2 + 3 + 1 = 6 (not 12).

Re-read: Lesson 0.1.2 — The toolchain: preprocessor, compiler, assembler, linker, loader

Q4 Answer: (A) (B) (D)1 mark

The preprocessor works on text before the compiler; it knows nothing about types.

Re-read: Lesson 0.1.2 — The toolchain: preprocessor, compiler, assembler, linker, loader

Q5 Answer: (A)1 mark

The loader is the operating system’s job of placing a program in memory and starting it.

  • (B) is wrong: That is the compiler.
  • (C) is wrong: That is the linker.
  • (D) is wrong: That is the preprocessor.

Re-read: Lesson 0.1.2 — The toolchain: preprocessor, compiler, assembler, linker, loader

Ready for the next idea?

Mark this item complete when you’ve finished reading and working through it.