A source code is the single form of program readable by human beings and understandable by computers. Generally, a computer program code is written in some high level programming languages such as C, C++, Fortran, Java, or any other which contains words and phrases from the English (or any other) language. But, a computer does not understand high level language as it only understands program written in binary form (in 0’s and 1’s), called the machine code.
Special softwares in the form of compilers and interpreters are used to translate the source code written in high level language into the executable and error-free machine code format. Though the major function of both of these is similar, there are numerous difference between compiler and interpreter.
A compiler and a interpreter both carry out the same purpose – convert high level language code into the binary form understandable by the computer hardware. These are basically softwares used to execute the high level instructions to perform tasks specified in the program.
Compilers and interpreter are specifically designed for different high level language platforms. Although compiler and interpreter both have the same primary objective, they differ in the way they accomplish it. So, they primarily differ in the way they convert high level language code into machine code. Through this post, we will discuss the basic working principles of both, and list out the basic difference between compiler and interpreter.
Compiler:
A compiler is a special type of program that translates a source code into its object code. High level programming languages come with a compiler of their own which defines the acceptable instructions. So, when a program code is written in C language, a specific compiler designed for C is used for compilation before the source code is executed. The compiler first scans the entire source code and translates it into machine code before it is executed by the computer processor, and then the tasks specified in the program are performed.
The first step in compilation is to pass the source code into the compiler so that it is translated into the object code. The compiler then sends object file (object code) into the linker from where the program comes out as an executable one. Intermediate code is not formed in compilation process. Compare machine code, object code and assembly code.
Interpreter:
Like a compiler, an interpreter is also a special kind of program for the conversion of a high-level program statement into machine code just before the program statement is to be executed. During each conversion, the interpreter converts the source code into an intermediate code before processing it into the machine for the generation of machine code. Each part of the code is interpreted line by line and then executed separately in a sequential manner. So, if an error is found in any part of the code, the interpreter will stop the interpretation without converting the next set of codes.
As interpreters check the source code line by line, translation and execution occurs immediately one statement at a time. As a result, they are 2 to 10 times slower than compilers. But this very disadvantage makes interpreters easier to user, especially for beginners; errors once found are immediately displayed and corrected by the user, before the program is executed. Listed below are some of the key features of interpreter:
- The input to the interpreter is a single instruction.
- Interpreter converts the source code into an intermediate code before processing it into the machine.
- During interpretation, the condition control statements execute slowly.
- Memory requirement is less.
- Errors found are displayed instantly.
- Since an interpreter is more interactive, it is used in teaching-learning sectors.
Compiler vs Interpreter:
- As already aforementioned, the major difference between compiler and interpreter is how they accomplish their primary objective. A compiler converts all the statements of source code into the object code and then finally into the exe file. On the other hand, an interpreter converts each statement of the source code into machine before execution of the statement. Interpreter does not produce any exe file.
- Once complied, a program does not need to be compiled again and again for the next run, whereas the interpreted program requires to be interpreted for each run of the program.
- Compiler takes the entire program as input, whereas interpreter takes only a single instruction as input.
- The memory requirement in case of compiler is more than that compared to interpreter.
- Another important difference between compiler and interpreter is that – in case of compiler, errors are displayed after the entire program is checked. But, in the case of interpreters, errors, if found, are displayed for every instruction interpreted.
- Compiler checks the source code at once, but interpreter checks the code line by line. In this respect, interpreters are considered to be 2 to 10 times slower than compilers.
- Before producing an executable file, compilers take some time, but interpreters can execute the code immediately. Taking this into consideration, compilers can be said to be slower than interpreters.
- Conditional control statements execute faster with compiler than with interpreter.
- Another remarkable difference between compiler and interpreter is that in compilation, the immediate editing and execution is considered to be a costly process because it takes longer time for the compilation of a program if the program is lengthy. On the other hand, with interpreters, it is possible to execute the edited statement immediately after its editing. So, interpreter is more interactive, easier to use, and is considered better for students.
- Compiler example: C compiler, C++ compiler, Fortran compiler, etc.
Interpreter example: LISP, BASIC, etc.
So, to sum it up, compilers and interpreters carry out the same objective, but they do it differently. Programming languages like C, C++ use compilers whereas others such as Python and Ruby use interpreters.
I hope this article helped you understand compilation and interpretation process as well in addition to the 10 basic difference between compiler and interpreter. Your queries relevant to this article can be discussed in the comments section below.