0 · Recall Warm-up
Answer from memory first. Answers are in the Answer Vault at the end of this lesson.
- When you type a message on your phone, does the phone’s chip understand the English words? from everyday life
- A cook gets a recipe written in a language she cannot read. Name two ways she can still cook it. from everyday life
- A tourist carries a phrasebook prepared before the trip; another tourist hires a guide who translates on the spot. Who talks faster during the trip? from everyday life
1 · The Story
📖
Mr. Patel books Translation House
Mr. Patel runs a textile business in Ahmedabad. Next week he must give the same speech every evening, for seven evenings, to an audience that understands only English. His speech is written in Gujarati.
He walks into Translation House. The director, Mrs. Desai, offers him two services.
Service 1 — the live interpreter. Raj stands next to Mr. Patel every evening. Mr. Patel says a sentence in Gujarati, Raj says it in English, then the next sentence. If Mr. Patel changes a line on Thursday, Raj adapts instantly. But Raj must work every single evening, and every evening each sentence is translated all over again. If a sentence in the speech is garbled, nobody finds out until Raj reaches it on stage.
Service 2 — the booklet. A translation team reads the whole speech once, before the first evening, and prints a complete English booklet. Garbled sentences are found and reported while the team works, before anybody is on stage. Every evening the audience simply reads the booklet, which is fast. But if Mr. Patel changes a line, a new booklet must be printed.
Mr. Patel asks: “Which is better?” Mrs. Desai smiles: “Seven evenings of the same speech? Print the booklet. A one-off chat with a buyer who keeps changing his mind? Hire Raj.”
2 · Predict First
🤔
A program will be run 1000 times on different inputs and is never changed. Which service from the story should a computer use for it: the live interpreter or the printed booklet? Write your guess down. The answer is in the vault.
3 · Core Concept
The problem: people write programs, processors run machine code
A program is a list of instructions written by a person in a programming language such as C:
total = price * qty + 50;
This is a high-level language: close to mathematics and English, and one line can mean many small machine steps.
The processor (CPU) does not understand this line. It executes only machine code: instructions encoded as patterns of bits (0s and 1s). Each machine instruction does one tiny step, such as “load a number from memory”, “multiply two numbers” or “store a number in memory”.
Machine code belongs to one processor family
An Intel/AMD (x86) processor and a phone’s ARM processor use different machine codes. The same C line becomes different bit patterns for each. The source is portable; the machine code is not.
So we need a translator
A translator is a program that reads a program written in one language and either produces an equivalent program in another language or carries out its actions directly.
The program we start from is the source program. A translated program that comes out is the target program.
Compiler
A compiler reads the whole source program and produces an equivalent target program, usually machine code, before anything runs. It also reports mistakes it finds in the source. The time when the compiler works is compile time.
Later, the target program (an executable) runs on its inputs as often as you like, with no compiler present. The time when the program runs is run time.
Formal definition. A compiler is a program that maps a source program in language S to an equivalent target program in language T, and reports errors in the source program.
Interpreter
An interpreter produces no target program. It takes the source program and the input together and carries out the statements one at a time, translating as it goes. A mistake in line 40 is found only when execution reaches line 40, after lines 1–39 have already run.
Comparing the two
| Column 1 | Compiler | Interpreter |
|---|---|---|
| Output | a separate target program | no target program; produces the program’s results directly |
| When translation happens | once, before running | every time the program runs |
| Speed of running | usually fast | usually slower (translation work during every run) |
| Errors in the source | reported at compile time, before running | found when execution reaches them |
| Changing the source | compile again | just run again |
| Typical examples | C, C++ (gcc, clang) | Python, shell scripts |
Hybrid. Java is compiled first into bytecode (code for an imaginary machine), and then a virtual machine interprets that bytecode, often compiling the busy parts to machine code while the program runs (just-in-time compilation).
Story → compiler term
| In the story | In the compiler |
|---|---|
| Mr. Patel’s Gujarati speech | source program |
| the English-only audience | the processor, which understands only machine code |
| the printed English booklet | target program (executable) |
| the translation team, working once before the evenings | compiler (compile time) |
| Raj, translating sentence by sentence on stage | interpreter |
| each evening’s performance | one run of the program (run time) |
| garbled sentences found before the first evening | errors reported at compile time |
| Mr. Patel changing a line | editing the source program: recompile (compiler) or just rerun (interpreter) |
4 · Visual
Compiler route, step 1 (compile time): translate the whole program once.
Compiler route, step 2 (run time): run the target program as often as needed, no compiler needed.
Interpreter route: every run translates again; no target program is ever produced.
5 · Worked Examples
Example 1 Easy
Classify each tool: (a) gcc, which turns a C file into an executable; (b) the python program running script.py; (c) a tool that turns C++ code into equivalent C code.
Solution
(a) Compiler: it produces a separate target program (machine code) before running.
(b) Interpreter (strictly a hybrid: it first converts the script to bytecode, then interprets the bytecode, and produces no separate executable you keep).
(c) A compiler too. Nothing in the definition says the target must be machine code. This kind is called a source-to-source compiler. In the story, it is like translating the Gujarati speech into a Hindi speech text: still a booklet made in advance.
Example 2 Medium
A program will run 1000 times. Compiling takes 2 s and each compiled run takes 0.01 s. Interpreting takes 0.5 s per run. Compare the total times. From how many runs onwards is compiling cheaper?
Solution
Compiler route: 2 + 1000 × 0.01 = 12 s. Interpreter route: 1000 × 0.5 = 500 s.
Break-even: compiling is cheaper when 2 + 0.01n < 0.5n, that is n > 2 / 0.49 ≈ 4.08. So from 5 runs onwards the booklet wins. This is Mrs. Desai’s advice to Mr. Patel in numbers: seven evenings of the same speech favour the booklet.
Example 3 GATE
Which statements are true? (i) An interpreter produces a separate target program. (ii) A compiler translates the whole program before execution. (iii) Interpreted programs usually run slower than compiled ones. (iv) A compiled program needs the compiler present every time it runs.
Solution
(i) False: an interpreter produces no target program; it executes directly. (ii) True. (iii) True: the interpreter repeats translation work during every run. (iv) False: once the target program exists, the compiler is not needed, just as the audience reads the booklet without the translation team in the hall.
Answer: (ii) and (iii).
6 · Shortcuts
⚡
One question decides it: does the tool produce a separate program that can run later without the tool? Yes → compiler. No → interpreter. Valid when: classifying a translator in an MCQ. Hybrids such as Java do both: compile to bytecode, then interpret.
⚡
Break-even runs: with compile cost C, compiled run time r and interpreted run time i, compiling is cheaper for n > C / (i − r) runs. Valid when: every run costs the same and i > r.
7 · GATE Traps
⚠️
- A compiler’s target need not be machine code: it can be another high-level language, bytecode or assembly-like text.
- An interpreter also detects errors, but only when execution reaches the faulty statement; earlier statements may already have run.
- Machine code is tied to a processor family. The source program is portable; the executable is not.
- Java is neither purely compiled nor purely interpreted: a compiler produces bytecode, then a virtual machine interprets (or just-in-time compiles) it.
8 · Memory Hook
🧠
Compiler = a booklet printed once. Interpreter = a live translator every evening.
9 · Connect the Dots
This lesson answered why translation is needed and the two ways to do it. Next question: when you compile a C program, the compiler is not the only tool involved. Which other tools touch the program before it runs, and in what order?
10 · Self-Test
GATE format. Try every question before opening the vault.
Q1MCQ1 markCompiler
Which statement best describes a compiler?
- (A) It executes the source program line by line together with the input.
- (B) It translates the whole source program into an equivalent target program before execution.
- (C) It converts machine code back into the original source program.
- (D) It only checks the spelling of the source program.
Q2MSQ1 markInterpreter
Which of the following are true of an interpreter?
- (A) It does not produce a separate target program.
- (B) A mistake in a statement is found only when execution reaches that statement.
- (C) Programs generally run faster under an interpreter than as compiled code.
- (D) The source program is needed every time the program runs.
Q3NAT2 marksBreak-even
Compiling a program takes 3 s; each compiled run takes 0.02 s. Interpreting takes 0.32 s per run. What is the smallest number of runs for which compiling (including its 3 s) takes strictly less total time than interpreting?
Numerical answer: ________
Q4MCQ1 markMachine code
Machine code produced for an x86 processor:
- (A) runs unchanged on any processor
- (B) is specific to the x86 family of processors
- (C) is the same text as the C source program
- (D) must be run by an interpreter such as Python
Q5MCQ1 markClassification
A tool reads a C++ program and writes an equivalent C program. This tool is:
- (A) an interpreter
- (B) a compiler
- (C) a processor
- (D) a text editor
11 · Rule Card
- Compiler: whole source → equivalent target program, before running; reports source errors at compile time.
- Interpreter: source + input → results directly, statement by statement; no target program.
- Compiled code is usually faster; interpreted code is easier to change and test.
- Break-even: compile when the number of runs n > C / (i − r).
- Target can be machine code, assembly-like text, bytecode or another high-level language.
12 · Concept Ledger
programhigh-level languagemachine codeprocessortranslatorsource programtarget programcompilerinterpretercompile timerun timeexecutablebytecodevirtual machine
13 · Answer Vault
Answer Vault
Try the questions first. Open when you are ready to check.
Warm-up
- No. The chip only acts on patterns of 0s and 1s. Some program has to convert what you typed into those patterns.
- Someone stands beside her and translates each line as she cooks, or someone translates the whole recipe into a booklet first and she cooks from the booklet.
- The phrasebook tourist. The translating work was done before the trip, so nothing has to be translated while talking.
Predict First
The printed booklet: translate once, before running, then every one of the 1000 runs is fast. That is a compiler. Example 2 shows the numbers.
Self-Test
Q1 Answer: (B)1 mark
A compiler maps the whole source program to an equivalent target program at compile time; the target then runs separately.
- (A) is wrong: That describes an interpreter.
- (C) is wrong: That is reverse engineering, not compiling.
- (D) is wrong: Checking is only one part of its job; it must also produce the target program.
Re-read: Lesson 0.1.1 — Programs, machine code and why we translate; compiler vs interpreter
Q2 Answer: (A) (B) (D)1 mark
An interpreter executes the source directly, finds errors on reaching them, and needs the source each run. It is usually slower, so (C) is false.
Re-read: Lesson 0.1.1 — Programs, machine code and why we translate; compiler vs interpreter
Q3 Answer: 112 marks
Need 3 + 0.02n < 0.32n, so 0.30n > 3, n > 10. The smallest integer is 11 (at n = 10 both take 3.2 s).
Re-read: Lesson 0.1.1 — Programs, machine code and why we translate; compiler vs interpreter
Q4 Answer: (B)1 mark
Machine code is the bit-level instruction format of one processor family.
- (A) is wrong: Other families (ARM, RISC-V) use different instruction encodings.
- (C) is wrong: The C source is human-readable text; machine code is bit patterns.
- (D) is wrong: Machine code runs directly on the processor.
Re-read: Lesson 0.1.1 — Programs, machine code and why we translate; compiler vs interpreter
Q5 Answer: (B)1 mark
It translates a whole source program into an equivalent target program: a (source-to-source) compiler.
- (A) is wrong: It does not execute the program.
- (C) is wrong: A processor runs machine code; it does not translate programs.
- (D) is wrong: An editor lets a person change text; it does not translate.
Re-read: Lesson 0.1.1 — Programs, machine code and why we translate; compiler vs interpreter