How does a computer understand a piece of code?

This article is intended purely for beginners who are getting started to understand and write code on their own.

People who are experienced in writing code will laugh and say, "Dude! common, it is all the compiler or an interpreter which performs magic in making the computer understand the code".

Credits of Cover Image - Photo by Joshua Sortino on Unsplash

Yes, the compiler or an interpreter plays a huge role in converting the code in 0s' and 1s'. You heard it right, the computer is only able to understand 0s' and 1s' which either way can be represented as True for 1 and False for 0.

computer-machine-code.png

What is Compiler?

We can think of Compiler as a program that is intended to translate or convert the written code into machine code or byte code which is then used by the system to execute the program and display the output. The compiler converts the source code (written program) into machine code in one single go. Often times, machine code is also termed as object code or byte code, or executable code.

compiler-image.png

What is an Interpreter?

Like the compiler, the interpreter is also a program whose main aim is to convert the written code into machine code. But, the conversion happens on the line by line basis, not in one single go.

interpreter-image.png

Note - Both compiler and an interpreter perform the same job except that compiler converts the source code (written program) into machine code in one single go whereas an interpreter does it on a line-by-line basis.

Real-life scenario

To understand this, in more detail - we can take a real-life example that we might have experienced at some point in time. Suppose you are suffering from a fever, you go to a doctor for getting treated. The doctor will check and gives a prescription. Now, when you try to understand what he has prescribed, you will never be able to contemplate that as the handwriting of the doctor would be hard to understand. But, if you take the same prescription and consult the nearby chemist, he/she will be able to give you proper medicines and advice you when and how to consume.

In this scenario,

  • Doctor is the programmer.
    • The doctor is writing a program (prescription) to solve your problem.
  • You are the computer.
    • You only understand English which is written normally as how computers only understand 0s' and 1s'.
  • Chemist is the compiler or an interpreter.
    • Chemist is helping you in the translation of the prescription into medicines.
  • You will execute (consume) the medication as how computers execute the actions once the source code is compiled or interpreted.

End

Well, that's it. This article gives a brief idea of the terms compiler and an interpreter and how they both play a major role in converting or translating the source code into machine code, which is later on executed.

No Comments Yet