Compiler vs interpreter

Mohamed Kamal
5 min readSep 16, 2022

--

After learning about programming languages, and knowing that computers only understand low level language or machine code, while high level languages are more human friendly and readable than machine code, and how we need a translator that translates our instructions from the high level language we could understand to the machine code that computers could understand, and we mentioned that in the computer world the translator is called compiler or may be called interpreter, now we are about to answer an important question.

What is the difference between compiler and interpreter, and when to use a compiler, and when to use an interpreter for translation to machine code language, or in other words, translating programming language from human readable to computer readable.

The above question leads us to a subclassification of high level languages, where high level languages can be subclassified according to the way they are translated to low level language or machine code into:-

  1. Compiled programming languages
  2. Interpreted programming languages

Compiled programming languages are sort of high level languages that use a translator called compiler to translate programmer instructions written in high level language into instructions written in low level language or machine code, so that a computer could understand those instructions.

where the file that contains the high level language instructions written by the programmer is called source code file.

So for example suggest we have a source code file that has some instructions that tell the computer to do some tasks as to print some words on the screen, so to translate that file into machine code the source code file enters the compiler program and the output of the compiler will be another file that’s written in machine code which is low level language which now computer could understand

s

So if we take that file to a computer and run it, we could see the program output.

That means, the computer understands it now.

But will you run that program on windows or mac or linux, because it matters, where they are different platforms, where the term platform means operating system so windows operating system is a platform, mac os is a platform, android is a platform and so on.

And however, the machine code is the language that computers understand, but each computer operating system or platform has its own specifications that differ from other platforms, so for that reason each platform will has its own program version that could deal with its unique specifications but cannot with other platforms of different specifications.

So for that reason programs that translated from high level to machine code language using compiler are called platform dependent programs, since the machine code file that the compiler outputs will be targeted for a specific platform or operating system

But on the other hand we have interpreted programming languages, where for interpreted languages the translator is called interpreter, but it’s a little bit different from compiler, where instead of translating the source code file into another machine code file for a specific platform. Each platform will has its own interpreter program that just takes in the source code file written in high level language.

Then that interpreter translates or interprets the instructions in that file directly to the machine code that’s suitable for the computer current operating system or platform, so it could understand that machine code.

Where the interpreter acts as instant language translator or interpreter, where it translates the high level language to low level language specific for the current platform, so what’s cool about interpreted languages is that you do not have to care about compiling the source code to a machine code specific for each platform, but instead you only pass the same source code file written in high level language to any platform and each platform will has its own interpreter program that will translate or interpret the high level language instructions into machine code immediately to the computer, a line-by-line, where the interpreter will first interpret the first line in the source code file to the computer into machine code and outputs the result.

Then move to the second line in the source code file and interpret it.

Then the third line and so on and so forth.

But now there are a couple of disadvantages here, first the interpreter program makes the interpretation process from source code to machine code every time the program runs, where that interpretation is done line-by-line and that’s make the program running slow if compared to the programs made with compiled languages, while the compilation or the translation process from source code to machine code is done only once and then the machine code file will run directly without translation anymore.

And the second issue is that if you develop a program to a client in an interpreted language that means you have to send the client the source code file to use which is more human readable and hence human editable, which makes your source code public for everyone and that’s not a good thing.

So, to summarize, the program written using compiled languages runs at high speed since what runs on the computer is a file with a ready pre-translated or pre-compiled machine code inside where the program runs directly as a whole, and also what’s cool here is that your source code is private and what runs on client computer is the only unreadable machine code file, but what’s not cool is that the program file only runs on one platform it was compiled for.

But on the other hand, the program written using interpreted languages can run on any platform since it runs through a program called interpreter which will interpret to the computer in its own machine code language what’s inside that source code file, where now you don’t have to care about which platform will run your program since any platform that has its own interpreter can run your program file, but what you pay in return is that now your source code file is public to anyone that runs your program on his computer, and also other disadvantage is that the process of interpretation to computer is done line-by-line which makes the program running slow if compared to those programs that are ready pre-compiled or pre-translated to machine code.

--

--