What is the difference between a compiler and an interpreter?

A compiler takes your entire program and translates it into machine code all at once before it runs. An interpreter, on the other hand, translates and executes your code line-by-line while the program is actually running.

Imagine you have a book written in French, and you only speak English. A compiler is like hiring a professional to write a complete English version of the book. You wait a while, but then you have a finished English book you can read quickly anytime. An interpreter is like having a friend sit next to you, translating the French text out loud, sentence by sentence, as you read.

How Compilers Work

When you write code in a compiled language (like C++ or Rust), you run a special program called a compiler. The compiler reads every single line of your source code and translates the whole thing into a new file of machine code (binary). This creates an executable file (like a .exe on Windows). Because all the translating is done ahead of time, the final program runs very fast. However, if you make a tiny change to your code, you have to recompile the entire program before you can see the change.

How Interpreters Work

When you write code in an interpreted language (like Python or JavaScript), there is no separate step to build an executable file. Instead, you just run the code. The interpreter software looks at the first line of your code, translates it into machine instructions, and runs it immediately. Then it moves to the second line. Because it translates on the fly, interpreted code usually runs slower than compiled code. But it is much faster for developers to test and debug, because you don't have to wait for a compiler to process the whole file.

Where Students Slip Up

A common mistake is assuming that a programming language is strictly one or the other. In reality, compiling and interpreting are just ways to execute code. While C is almost always compiled and JavaScript is almost always interpreted, many modern languages use a hybrid approach. For example, Java compiles your code into an intermediate format called "bytecode." Then, a Java interpreter (the Java Virtual Machine) runs that bytecode. Python does this behind the scenes, too!

Worked through

Suppose you write a two-line program. Line 1 prints "Hello, World!" to the screen. Line 2 contains a major syntax error. How will the execution differ if you use a compiler versus an interpreter?

If you use a compiler, it will scan the entire file before running anything. It will find the syntax error on line 2, print an error message, and fail to create an executable. Nothing will be printed to the screen.

If you use an interpreter, it will execute the code line-by-line. It reads line 1, translates it, and successfully prints "Hello, World!" to the screen. Then, it moves to line 2, encounters the syntax error, and the program crashes.

Questions students ask

Ask about this topic

Where this comes from: OpenStax Computer Science Principles · Khan Academy: Computers and the Internet · Structure and Interpretation of Computer Programs by Abelson and Sussman

See also