Introduction to
Compilers and Interpreters



What are Compilers and Interpreters?

When we write a program using any programming language, the computer doesn’t understand it directly. Computers only understand machine code—binary (0s and 1s). So, we need a translator to convert our human-readable code into machine-readable code.

This is where compilers and interpreters come in. Both are types of language translators that serve this purpose, but they work differently.

Compiler

A compiler reads the entire program, translates it into machine code, and creates a separate executable file. This executable can be run directly by the computer without needing the compiler again.

Example:

Suppose you write a program to add two numbers:

START
  READ A, B
  SUM = A + B
  PRINT SUM
END

When you use a compiler:

Question:

Why does a compiler show all the errors at once?

Because it processes the whole code in one go. It builds a full understanding of your code before running it, which allows it to find and report multiple issues in a single pass.

Interpreter

An interpreter translates and runs the program line-by-line. It reads each line, converts it to machine code, and executes it immediately before moving to the next.

Example:

PRINT "Enter number A"
READ A
PRINT "Enter number B"
READ B
SUM = A + B
PRINT "Sum is", SUM

If this is run using an interpreter:

Question:

Why is interpreting slower than compiling?

Because each time the program runs, the interpreter must re-read and re-translate each line. In contrast, compiled code runs directly since it's already translated.

Comparison Table

Aspect Compiler Interpreter
Translation Whole program at once Line by line
Error Reporting All errors after compilation Stops at the first error
Speed Faster execution (after compile) Slower due to real-time translation
Output Creates an executable file Does not create executable

Pseudocode Simulation

Compiled Version:

PROGRAM SumCalculator
  READ A, B
  RESULT = A + B
  PRINT RESULT
END PROGRAM

Output:

(Compiler creates executable)
User inputs: 10 and 20
30

Interpreted Version:

PRINT "Enter first number"
READ A

PRINT "Enter second number"
READ B

RESULT = A + B
PRINT RESULT

Output:

Enter first number
(Reads user input)
Enter second number
(Reads user input)
30

When to Use What?

Question:

Is it possible for a language to use both?

Yes! Some languages are compiled to an intermediate form and then interpreted (e.g., bytecode interpreted by a virtual machine).

Summary



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

Mention your name, and programguru.org in the message. Your name shall be displayed in the sponsers list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M