What is the difference between Compiler and Interpreter?

What is the difference between Compiler and Interpreter?

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.

Table of Content:

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.)

 

difference between Compiler and Interpreter

 

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.

Intermediate code is not generated by the interpreter. So it is less prone to error at execution time. As it executes code one by one statement, the run-time errors can occur. 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.Language program that needs more flexibility and programmability uses Interpreter. Most of the language that uses interpreter are dynamic and scripting languages.

Compiler  Interpreter
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.

Java compiled or interpreted

You may also like to read some more differences:

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.

4 Comments

  1. Aniruddha – your efforts here are great and much appreciated. Thank you. I have also watched your Youtube video about clearing the CMD module screen. Such a clever, simple way 🙂
    Thanks again. I will work through this with my books by Al Sweigart.

    Kean to learn this to add to my machine language programming, BASIC & QB4.2 as well as QB64 to use on my HP Laptop with INTEL i7 processor. I have also written programs in dBase 3+ and also DOS scripting/Batch files.

    That should keep my old brain active. Thank you again for doing this all on the Internet for us all to benefit from.

    1. Thanks for your kind words, Wolfgang. And I’m glad you enjoy reading/watching my content. It keeps me going.

      Good to know your area of interest. Feel free to reach out to me if you would like to share your knowledge with our readers.

      Meanwhile, can you spare a minute or two, to share your feedback or testimonial? I would like to feature it on our website. Thanks again.

Leave a Reply

Your email address will not be published. Required fields are marked *