The Difference Between Compiled and Interpreted Languages

Different language types yield varying efficiency and ease of coding

Computer code in various colors

Juhari Muhade / Getty Images

Programming languages break into two different families: compiled and interpreted. A compiled language is coded by a human, then that source code translates into assembly language so that the target program runs and returns a desired result. An interpreted language, however, is compiled in real time when it's run, and it often uses simpler and more human-friendly syntax for coding.

The choice of a compiled vs. interpreted language shouldn't be a major factor when you're considering a new programming language to learn.

What Is a Compiled Language?

Hello World in C

Consider a simple program, helloworld.c, written in the C programming language:

#include <stdio.h>
int main()
{
      printf("Hello World");
}

C is an example of a compiled language. To run the above code, you must pass it through a C compiler—a program that interprets your code to construct a binary program. To compile a simple C program in Linux, use the gcc compiler:

gcc helloworld.c -o hello

The above command turns the code from human-readable format into machine code that the computer can run natively. A compiled program runs by executing the name of the compiled program from the shell:

./hello

Strengths and Weaknesses

The benefits of using a compiler to compile code is that the final product generally runs faster than interpreted code because it doesn't need to compile on the fly while the application is running. 

The compiled program was checked for errors during compilation. If there are any commands that the compiler doesn't like, then they will be reported. This error-checking helps you fix all the coding errors before you've got a well-running final product.

Just because a program has compiled successfully doesn't mean that it will run the way you expect, so you still need to test your application.

However, a C program compiled on a Linux computer will not run on a Windows computer, because the compiler includes the necessary tools to run the application on a specific platform—usually, the platform upon which it was compiled. To get the same C program to run on a Windows computer, you must compile the program again using a C compiler on a Windows computer.

What Is an Interpreted Language?

Hello World in Python

Python is a popular interpreted language.

print ("hello world")

The above code is a python program that will display the words hello world when it is run. To run the code we do not need to compile it first. Instead, we can simply run the following command:

python helloworld.py

Alternatively, just type it from the Python interactive interpreter.

This tiny one-line program does not need to be compiled first but it does require that Python is installed on any machine that runs the script. The Python interpreter takes the human-readable code and turns it into something else before making it something the machine can read. All of this happens behind the scenes and as a user, all you see are the words hello world printed to standard output.

Strengths and Weaknesses

In general, interpreted code runs more slowly than compiled code because the interpreter translates the program on-the-fly into something the machine can handle.

Although this efficiency hit might seem like a downside, interpreted languages are useful for several reasons. First, it is much easier to get a program written in Python to run on Linux, Windows, and macOS. All you need to do is make sure Python is installed on the computer you wish to run the script.

Another benefit is that the code is always available for reading and it can be easily changed to work the way you want it to. With compiled code, you need to find where the code is kept, change it, compile it, and redeploy the program. With interpreted code, however, you open the program, change it, and it is ready to go.