Michael Eghan
last year
Michaelcountry asked

What makes a compiler and an interpreter different.

Kelish Rai
Expert
last year
Kelish Rai answered

The difference between a compiler and an interpreter mostly comes down to how they translate our code into machine-understandable language:

  • A compiler converts the entire code at once, creating a separate file (like an executable) that can be run later. Once compiled, you don’t need the source code to run the program.

  • An interpreter, on the other hand, translates the code line by line as it runs the program. It doesn’t produce an executable file; instead, it processes the code directly during execution.

This difference affects several aspects of how the code is executed, like execution speed and error handling.

Here's an example to make the difference clearer:

1. Compiled language (e.g. C):

// hello.c
#include 

int main() {
    printf("Hello, world!\n");
    return 0;
}

You would first compile this using a compiler like gcc:

gcc hello.c -o hello

Then run the compiled file:

./hello

2. Interpreted language (like Python):

# hello.py
print("Hello, world!")

You just run it directly with the Python interpreter:

python hello.py

Note: Some modern languages use both techniques. For example, Java code is compiled into bytecode, which is then interpreted (or further compiled) by the Java Virtual Machine (JVM).

Python
This question was asked as part of the Getting started with Python course.