What Are Compiled Vs Interpreted Languages?

What Are Compiled Vs Interpreted Languages?
What Are Compiled Vs Interpreted Languages?

Introduction

A computer program is a set of instructions that tells the computer to perform a specific task. Every program, whether it’s as simple as adding two numbers or as complex as sending the request over a web server, is written in High-level language like Java, Python, C.

These high-level languages closely resemble English and follow some basic syntactical structure to write and execute, these programs are then run over the Computer to give output. The process seems relatively simple only if the Computer can understand high-level languages. But the Computer only understands Machine language(made up of 0s and 1s). 

In simple terms, consider that you only understand English and someone is speaking to you in french. You are unable to process what the person speaking in french is trying to convey. So, to understand French, you hire a person who will do the translation job for you, i.e., translating French to English.


That’s what compilers and interpreters do; they take the program written in a high-level language and translate it to machine languages. Read out the full blog for more information and get a clear explanation of Compiled vs Interpreted Languages.

Program Execution

#include<stdio.h>      // This line will execute first
int main()                   // This line will execute second
{
      printf(“Compiled vs Interpreted Languages”);
      return 0;
}

Consider the above program wherein a message, “Compiled vs Interpreted Languages”, is displayed to the programmer. The source code is written in C Programming Language, which is a high-level language. The above program will execute sequentially, i.e., one line is executed only after the previous one has completed its execution.

But as we saw in the example of French to English translation, and from the fact that computers only understand the language of 0s and 1s, it can be inferred that this program cannot execute directly on the Computer; this needs to be translated to Machine Language to be able to run on the Computer.

High-Level Language                                       

#include<stdio.h>
int main()
{
        printf(“Compiled vs Interpreted Languages”)
        return 0;
}

 Machine Language

0000001111110001111
0000001111100000111
00000000111100000111
…………………………….
…………………………….
…………………………….

Compilers and Interpreters handle the job of translation.

In the example of French to English Translation, the person we hire to do the translation can do it in two ways:

  • Translation Line by line: The person you hired to translate will listen to each sentence of the conversation and will translate it on the go.
  • Translation of the whole conversation at once: The translator will first listen to the complete conversation and then translate.

Translation Line by Line is what an Interpreter does. Translation of the whole program at once is what a compiler does.

Based on how the translator works, i.e., either line by line or complete program in one go, the Programming Languages can be categorized as Compiled Languages and Interpreted Languages.

Compiled Languages

Compiled Languages make use of the Compiler to translate high-level source code into Low-level Machine code. The Compiler will scan the complete source code at once and then generate a binary file called executable code or object code (which is undecipherable by humans). The source code is directly translated into Machine code that the processor can execute. 

How does it work?

  • A compiler does the job of translation and generates an executable file of the source code. When executing (running), the Compiler first parses (or analyzes) all of the language statements syntactically one after the other and then, in one or more successive stages or “passes,” builds the output code, making sure that statements that refer to other statements are referred correctly in the final code.
  • This executable file can be directly shared with others without needing to share the source code. This makes the source code private. 

  • The machine in which we share the executable file should have the same architecture as the machine in which it was initially built, as the executable file is architecture-specific. A source code compiled on Windows cannot be executed on a device with Linux operating system.

Due to the architecture-specific nature of the compiled code, there are different compilers for the various operating systems and different architectures of the system.

Pros of compiled Languages

  • In compiled languages, only the executable file is shared with others. This keeps the source code private.
  • A compiled language is translated directly into Machine code, and this makes compiled languages faster.
  • A compiled program or an executable file is ready to run. There is no need for additional steps once it’s compiled successfully.
  • Target programs execute independently and do not require the Compiler in the memory.

Cons of compiled languages

  • Separate compilation needs to be done for different operating systems and different architectures of the same operating system. So, the compiled languages are not cross-platform.
  • The entire program needs to be re-compiled even with just a small change in the source code. This makes it inflexible.
  • The compilation process is complicated; a lot of time is spent analyzing and processing the program.
  • All the errors and warnings will be displayed at compile-time, so the program cannot run without fixing errors.

Examples of Compiled Languages are C, C++, Rust, Go, and Haskell.

Interpreted Languages

Like a compiler, an Interpreter translates the source code written in a high-level language to machine language directly. An interpreter also translates but, in a different manner, An interpreter will run through the program line by line and execute each line on the fly. A programming language that uses an Interpreter for translation purposes is called an Interpreted Language.

An example is, whenever you open a website with JavaScript, the JavaScript is sent to you over the web along with a bunch of other files like images and webpages and is being rendered as source code to your machine, and the web browser interprets it on the fly.

Note that, unlike compiled languages, interpreted languages do not save the object code file. This is also a major difference between Compiled vs Interpreted languages.

How does it work?

  • Source code is written and interpreted on the programmer’s system.
  • The source code can be shared directly with others as long as they have an interpreter for the programming language. The user’s Computer will interpret the source code line by line and will display the output.

  • A significant advantage of using an Interpreted Language is that it can work anywhere; as long as the device in which it is intended to run has an interpreter for the language, it can interpret the source code and run the program. So a program written in Windows can load successfully in MacOS as well.

An interpreted program is ready to run on all types of machines.

Pros of Interpreted Languages

  • It can be run on any machine as long as the Computer has an interpreter for it. An interpreter language is cross-platform.
  • It’s simpler to test and develop as errors are reported to line by line.
  • Interpreted languages support dynamic typing and don’t save the object code as a separate file.

Cons of Interpreted Languages

  • An interpreted language is at least ten times slower than compiled languages.
  • An interpreter is required in the machine in which it is intended to run.
  • Source code is public as it is shared with everyone who needs to run that program.

Examples of Interpreted Languages are JavaScript, Perl, Python, BASIC, etc.

Now you have a clear understanding of compiled and interpreted languages, now let’s look at the differences between Compiled vs Interpreted languages.

Difference between Compiled and Interpreted Language

The differences between Compiled vs Interpreted languages are summarised in the table below: 

Compiled LanguagesInterpreted Languages
Compiled Languages uses the Compiler for translation purposesAn interpreter language make use of an interpreter for translation purpose
In this language, compiled programs are usually faster to execute than interpreted programsInterpreted programs are generally much slower than compiled programs.
At least two steps are required to get from the source code to the execution phase.There is only a single step required.
In compiled languages, even a single error will stop the execution of the complete programIn an interpreted language, all the code before the error will execute.
The code of compiled language can be executed directly by the Computer’s CPUA program written in an interpreted language is not compiled, it’s interpreted line by line.
Compiled languages deliver better performance.An interpreted language delivers relatively slow performance.

Frequently Asked Questions

Is Python a compiled or interpreted language? What is the difference?

Python is neither truly a pure compiled type nor a purely interpreted language, but it is called an interpreted language because it interprets the .pyc file

Source code written in Python programming language (.py extension) is first compiled to bytecode (.pyc extension), This bytecode can be interpreted (official CPython) or JIT Compiled (PyPy). So effectively, the compilation part is done when the first time source code is executed, a bytecode is generated, and internally this byte code gets converted by Python Virtual Machine according to the Platform.

Are all interpreted languages compiled?

Not all interpreted languages are compiled. Languages like JavaScript are directly interpreted by the web browser while some languages like Python use Compiler and Interpreter both.

Is C++ an interpretive language?

C++ is a compiled language because it uses a compiler to translate the source code into an object code. This object code is converted to the executable for the end-users to run.

What is a compiled language?

A compiled language is a programming language that uses a compiler for translating the source code into machine code. The code of compiled language can be executed directly by the Computer’s CPU.

Why is C called compiled language?

C is called compiled language because it uses a compiler for the translation of source code to machine code. The .c file is converted to a .exe file which can then be executed directly by the Computer’s CPU. Also, a .c program compiled in one system cannot be executed in another system if the second system has a different architecture.

Why is Python called an interpreted language?

Python is neither truly a pure compiled type nor a purely interpreted language, but it is called an interpreted language because it interprets the .pyc file generated by the interpretation of byte code. (The Compiler generates the byte code).

Key Takeaways

This blog covered an in-depth analysis of compiled and interpreted programming languages. The blog aimed to give a clear understanding of how compilation and interpretation work and the main differences between Compiled vs Interpreted languages.

With this done, you can now figure out more details about compilation, how a compiler works, and how it generates the byte code. Don’t stop here, do check out Coding Ninjas for amazing courses and guided paths.

Also, try CodeStudio for more interesting articles, interview experiences, and amazing Data Structures and Algorithms problems.

Remember, “Success is the sum of small efforts repeated.” So, BE CURIOUS!

By Manvi Chaddha

Exit mobile version