Difference between Compiler and Interpreter

“What is the difference between compiler and interpreter?”, this used to be one of the often asked question during my computer science courses. If you’re learning programming, you would definitely need to compare compiler vs. interpreter and learn the difference. Unfortunately, most of the computer science students tend to just mug up the answer without truly understanding the concept. Even a few teachers don’t know the concept of compiler and interpreter so well as to teach their students!

This morning, while speaking with one of my team members, I once again remembered this classic compiler vs. interpreter comparison and I immediately decided to write a small piece about it. This article will be useful for all the programmers wannabes.

What is a Compiler?

First of all you need to understand that computers and humans speak different languages. While humans speak languages like English, French, Hindi, Spanish, Chinese, Japanese, Arabic etc.; computers speak only one language i.e. binary. Alphabet of computers contains only two letters, namely zero and one. Computers are machines and for them it is much easier to work using a simpler encoding of 0s and 1s.

We humans feel most comfortable if we could issue instructions by writing plain English  or Hindi programs for computers.

The problem is that we are not comfortable with binary and computers find our languages too difficult to understand. To get around this problem a middle path of an intermediate language is used to convey human commands to the computers. This intermediate language is called —the programming language.

Programming language is not plain English. It is essentially a predefined syntax of writing logical computer instructions which humans can also understand.

We also need something to convert programming language into binary language for the consumption by computer. This is where compiler comes in scene.

Compiler is a software that checks a computer program (called source code) and lists all the errors found. When humans remove all these errors, compiler converts the program into binary code (also known as machine code or object code). Computers can understand binary code and therefore they can execute instructions written by humans using programming language.

Source code and assembly code.
Source code and assembly code.

The following video will further help you in understanding the concept and working of a compiler:

Compiler vs. Interpreter

Now we already know what a compiler does. Task of interpreter is also more or less the same but interpreter works in a different fashion. The difference between the functioning of compiler and interpreter will be clear from the table of comparison given below:

#CompilerInterpreter
1Compiler works on the complete program at once. It takes the entire program as input.Interpreter program works line-by-line. It takes one statement at a time as input.
2Compiler generates intermediate code, called the object code or machine code.Interpreter does not generate intermediate object code or machine code.
3Compiler executes conditional control statements (like if-else and switch-case) and logical constructs faster than interpreter.Interpreter execute conditional control statements at a much slower speed.
4Compiled programs take more memory because the entire object code has to reside in memory.Interpreter does not generate intermediate object code. As a result, interpreted programs are more memory efficient.
5Compile once and run anytime. Compiled program does not need to be compiled every time.Interpreted programs are interpreted line-by-line every time they are run.
6Errors are reported after the entire program is checked for syntactical  and other errors.Error is reported as soon as the first error is encountered. Rest of the program will not be checked until the existing error is removed.
 7A compiled language is more difficult to debug.Debugging is easy because interpreter stops and reports errors as it encounters them.
 8Compiler does not allow a program to run until it is completely error-free.Interpreter runs the program from first line and stops execution only if it encounters an error.
 9Compiled languages are more efficient but difficult to debug.Interpreted languages are less efficient but easier to debug. This makes such languages an ideal choice for new students.
10Examples of programming languages that use compilers: C,  C++, COBOLExamples of programming languages that use interpreters: BASIC, Visual Basic, Python, Ruby, PHP, Perl, MATLAB, Lisp

Following image shows how different languages are either compiled or interpreted:

Difference between compiler and interpreter.
Difference between compiler and interpreter.
Difference between Compiler and Interpreter

So, what did we understood from the above table of comparison? We now know that the reason a compiled runs faster because it gets converted into machine code all at once. Computer can easily understand machine code and therefore can run the whole program quickly. But this also means that the entire compiled code has to reside in the memory —and thereby making the compiled programs more memory guzzling. With memory available in abundance, nowadays, this is no longer a concern. But in earlier days of programming, when memory was an expensive resource, program size up to every bit and byte used to matter.

In comparison, interpreter is a more human-friendly program. It works quite a bit like humans work. It begins program execution right at the first line and will go on showing you the result of the program until it will hit an erroneous line of code. As a result, for programmer it is much easier to locate error and to understand the nature of error. However, interpreter is slow. An interpreted programs gets interpreted every time it runs.

Java is Both a Compiled and Interpreted Language

Ah! Java is an amazing programming language. Designed originally by James Gosling, this language encompass several innovative features. One of such features is Java Virtual Machine (JVM). Also, Java is both a compiled and interpreted language. When you write a Java program, the javac compiler converts your program into something called bytecode. All the Java programs run inside a JVM (this is the secret behind Java being cross-platform language). Bytecode compiled by javac, enters into JVM memory and there it is interpreted by another program called java. This java program interprets bytecode line-by-line and converts it into machine code to be run by the JVM. Following flowchart shows how a Java program executes.

Execution of a Java program. Java is both a compiled and interpreted language.
Execution of a Java program. Java is both a compiled and interpreted language.

I hope this article helped you in understanding the concepts of compiler and interpreter. If you have any further questions on the difference between compiler and interpreter, please feel free to comment and ask me. I will most certainly try to assist you. Thank you for using TechWelkin!

34 thoughts on “Difference between Compiler and Interpreter”

  1. i have a few doubts regarding interpreter.it is written that interpreter does not generate an intermediate machine code but later on in the last para it is written that interpreter interprets byte code lineby line and generated machine code.so the interpreter is also generating machine code and compiler also generates machine code which is here called intermediate code.so whats the difference ??

  2. Hi sir,

    Thanks for explaining the concept in very simple way.

    I have few questions here ,when you say that the compiler will produce INTERMEDIATE machine code as per the difference 2 why is it referred as intermediate?

    Its ultimately the machine code which is the final format that the underlying platform understands right ?

    If interpreter is not generating machine code then what is its output/interpreted result?

    1. Code produced by compiler is often called intermediate code because that code may still go through further medications (such as optimization) before becoming ready for execution on the target machine.

  3. Why does java use both and what gets put into just in time compiler and java interpreter, that requires both of them? Is is because some of the bytecode is faster through the compiler but not all of it so it uses the interpreter or why?

  4. Sir,
    Thanks a ton….
    It was really helpful and brilliantly elaborated. Cleared a lot of doubts especially with respect to java.
    I would like to know about the role of Just In Time (JIT) Compiler.
    And, I am a beginner in Java , so if could kindly give me any contact of yours where I can clear my doubts, it would be of great help.

  5. K Mukesh Mahadev

    The most straight and necessary information provided.
    Thanks a lot Sir!!!
    The last part about java is now very clear to me.

  6. Thank You very much Sir. My question is : Are compiler and interpreter are inbuilt with windows operating system or we have to install both the software separately. If they are inbuilt with windows OS , then how to select one from both to check a source code.

    1. A compiler or interpreter comes as a package when you install a development environment for a programming language. For example, when you install Microsoft Visual Studio or Turbo C etc. the required compiler / interpreter also get installed.

  7. Sir, this article very helpful for understanding the difference between compiler and interpreter… Thank u so much..

  8. Sandeep Shrivastava

    Nice article …really helped me a lot …keep writing articles like this. It is the best explanation of compiler and interpreter read by me till now..

    1. Hi Mohit, compiler and interpreter do not really fit in the definition of system of application software. But if you have to categorize, then I would say that compiler is more like an application software because it does not play role when the compiled program is executing. In contrast, the continuous need of interpreter during execution makes it more like a system software.

  9. Dear Sir I am very thankful to you.

    You did a great job and your language is simple to understand. It doesn’t seem that it’s written in English; it’s like our mother tongue. Now I can differentiate very well between interpreter n compiler and tell anyone. As you said most of the teachers don’t know about this. You are absolutely right. Once again thanks a lot sir n keep doing this job.

    1. Thank you Danish! I am happy that this article helped you in understanding the difference. All the best for your future. Stay tuned with TechWelkin!

  10. Thanks for the article. But, I have a small question – Why people say that compiled programs are faster than interpreted ones?

    1. Good question, Dinesh! As I said in the article, interpreted programs needs to be interpreted everytime they run. That makes them slower. Also interpretation of conditional control statements is slower. In contrast, compiled programs get converted into ready-to-run binary code at the time of compilation. As a result, compiled programs run faster.

    1. Thank you for taking time out to write your kind words. I am glad that this article turned out to be useful for you. Stay connected with TechWelkin!

  11. Sharad Singh Sankhla

    जय श्री कृष्णा
    ललिलित जी मै कंप्यूटर की A B C D भी नहीं जानता पर आपने जो लिखा उसे पढ़कर यह समझ सकता हूँ कि ये जानकारी बहुत ही उपयोगी है .

  12. Shyamal Kumar Das

    Sir,

    I worked as a programmer for 15 years, without having such a through knowledge about compilers and interpreters. Simply I wrote programs in Clipper and compiled them to executable files after proper linking.

    Thank you very much for the knowledge, you shared.

    Regards.

Leave a Comment

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