What is the difference between Compiler and Interpreter?
In my early days, when I had asked, how should I learn computer science and do something innovative? I always had come across, start learning computer languages. In hustle and bustle, many forget, how does actually it work? How does computer program run?
If you are also looking for same, you are at right place. I am sure, at the end of the post, you will get very well understanding of compiler, interpreter and how actually it work to run your code.
I start with some brief prerequisite introduction. Basically, from human and machine point of view, there are high-level languages and machine level languages.
High-level language: It is written by the human (programmer). It is mostly like English type and can not be understood by the machine.
Machine level language: As Computer knows only 0 and 1 (on/off signal), a high-level language is not understood by the computer. So there is machine level language.
To run any program on the computer, you need some translator that will convert high-level language to machine level language. This translator is of two types compiler and interpreter.
Before knowing the difference between Compiler and Interpreter, just go through what is compiler and interpreter.
What is the Compiler?
The compiler is a translator that simply convert the program from one language to another language, from high-level language to machine level language. This conversion happens in the single shot and gives executable file. The process of compiling a code is called as a compilation. Execution is not carried out while compilation phase. You need to
Execution does not carry out while compilation phase. You need to run executable object separately.
What is the Interpreter?
The interpreter is radically different from the compiler. It reads the high-level program code line by line and translates it into machine level code and at the same time, it executes each line. (It does not wait for the complete program to translate.)
Note: Compiler and interpreter are itself software program, that converts the program from one language to other.
Now lets, keep it simple with below example.
How do actually Compiler and Interpreter work?
We keep this example generic. The program is nothing but the set of instructions.
Assume following are the instruction in the program that takes an integer value from the user and manipulates some mathematical operations. After all operations, it prints output.
- Take the user input as integer number (says n)
- Add 10 to number ‘n’
- Multiply it by 5
- Subtract 100
- Divide by 3
- Display the result
How does Program Run on Compiler?
In the above sample program, a very first instruction is to accept input from the user. Before getting input from a user, it compiles all the program code and saved formula something like as ((n+10)5 -100)/3. It generates the executable object file (machine level language program). This compilation process happens only once.
Now the job of the compiler is completed. Next job is to execute compiled target file.
The user needs to run executable object explicitly. When the user runs it, it asks for user input and saves in variable ‘n’. The value of ‘n’ gets substituted in the formulae. The result of the formula will be displayed as output to the user.
We can repeatedly run this program with multiple user inputs without any more compilation.
How does Program Run on Interpreter?
In a case of an interpreter, it goes through each instruction. For each instruction, it generates machine level code and executes. In above example, it translates first instruction and asks a user for input. It accepts the input and saved in variable ‘n’. The controller moves to next instruction and performs addition operation (n+10). Likewise, it follows all the instructions one by one and executes. The result from the last instruction will be displayed as output to the user.
I always love to keep it simple. If you are willing to learn, I have shared an article, how does computer network work in simple word.
Difference between Compiler and Interpreter:
There is a difference between compiler and interpreter, the way it executes the program.
|It scans complete program in one shot and translates it into machine level language.||It reads each statement at once and translates it into another language.|
|All the errors are spotted at compilation time. So it is less prone to error at execution time.||As it executes code one by one statement, the run-time errors can occur.|
|Errors are generated after analyzing the complete program. So it is hard to debug the compiled language code.||As it scans each statement, it throws an error if it occurs at any statement. So it is easy to debug interpreted language code.|
|As compilation happens only once, it runs faster while execution.||We need to interpret every time to run the program. Its execution is slower than the compiler.|
|The compiler is mostly used by the program that needs high performance.||Language program that needs more flexibility and programmability uses Interpreter. Most of the language that uses interpreter are dynamic and scripting languages.|
|Programming languages that use Compiler: Fortran, Pascal, C/C++, Ada||Programming languages that use Interpreter: Perl, Python, CL, Lisp, Scheme, Bash, AWK|
Is Java Compiled or Interpreted Computer Language?
This is the basic question, asks in many Java interviews.
While running java program, it gets converted into bytecode and then it runs. Conversion of java to byte-code is carried out by compiler called as javac compiler. Target bytecodes are interpreted using interpreter called as JVM (Java Virtual Machine). So Java is both interpreted and compiled language.
You may also like to read some more differences:
- Difference between Path and Classpath Environment Variable in Java
- Difference between Microprocessor and Microcontroller
In this post, I have tried my best to explain the difference between compiler and interpreter. Compiler and interpreter have its own advantages as well as disadvantages. It is very difficult to decide superiority of compiler over interpreter and vice-versa. If you have any question, feel free to comment bellow.