How do computers understand your code? Read the post now

Knowledge: How do computers understand your code? What really happens after you run your code?


So programmers you certainly know how to write a program to calculate the factor of a number or check if a string is a palindrome or not or even implement the Dicer shortest path algorithm by reading code but do you know-

 How your program is actually understood by a computer? Ever thought of it?

The most basic thing which you already know computers understand only binary data 

but what does that mean it means that computers can only deal with zeros and ones no other things but have you ever wondered why? 

Let me tell you your computers are made of circuits these circuits are nothing but a sequence of gates like and gate, or gate so by gate you definitely heard these names right? 


So all these gates are actually made of transistors now

 A transistor is what we can call a fundamental building block of a computer just like how a cell is a fundamental unit of life so in the simplest words-

 A transistor is what makes a computer work 


but what exactly is this transistor?  

It is a component that can basically hold two states the first state is on which we represent as one and the second state is off which we reversed as zero now it must make sense why computers can only deal with zeros and ones right now

Let's come into the big picture how we say that a program like -


This be understood and executed by your computer which only understands zeros and ones?
The most obvious answer is this code must be converted into a series of zeros and ones so that it actually makes sense to the computer.
Yes that is exactly what happens any program written in any programming language must eventually be converted into machine instructions so that your computer understands it but

How does that happen?

  • Well this conversion from source code to machine instructions is made possible by a set of magical tools together known as the compilation system to know more about how this conversion works let's actually take a step-by-step approach on how a program is written compiled and finally executed
  •  Let's consider the above simple C program which basically adds two numbers and prints the sum of these numbers first we save this program with a dot C extension let's say we save it as hello dot C the next step is the compilation we can use the GCC compile which is open source to compile this C program you open your terminal or command prompt and then type in GCC space hello dot C - o and then hello this will create an executable file named as hello which we can finally execute and when we execute it the computer actually execute the file and prints the output.
  • This means that at this stage the computer understood our code and it actually computed what is written inside the program so basically it added the two numbers and printed the output now.

 Let's break down what just happens initially when you save the hello dot C file on your computer it is actually saved as a sequence of bytes the program that you have written basically contains English alphabets special characters and numbers that's it.

Now in order to convert these individual tokens or characters into binary-


We first convert each character into a decimal format and go on to convert these decimal values into their binary format so most computers follow the ASCII standard to do the same

So basically, each character has its own corresponding numeric value for example the character hash(#) has the ASCII value 35 and the character small.I has the ASCII value 1.5 and similarly, all the characters have their own numeric value once the characters are converted into their ASCII values they are then converted into binary format,

Each character is represented using eight bits or in other words one byte now that everything is in binary this sequence of bytes can finally be saved on your computer's file system.

The next step is where you compile your program with GCC now this is the big step because in this step your source code is converted into the executable machine instructions there are


  • In the first step, your program is fed to the preprocessor which basically checks all the header files in your program and adds the code of all these header files into your program, and then saves this as an intermediate file named hello with a dot I extension so it's basically hello dot I for our program since we are using the STD i/o dot H header our preprocessor adds the code of stdio.h into our program this hello.I file is fed to the compiler which converts this source code into an assembly code and then 
  • saves it as hello dot i the assembly language is just a human-readable format of the machine code this step is actually optional because generating the assembly code from a source code is not necessary it is only generated because the programmer will be able to read and understand this assembly code and get to know what the processor will execute from a low-level this assembly code is then passed to the assembler this is a step where the assembly code is converted by the assembler into the machine instructions, in other words, the binary form 
  • It is then saved as hello dot o, at last, comes the linking phase in this phase a program called the linker checks if there are any dependencies in your code in our code since we are using the printf function which is defined in the standard c library the printf dot o file which contains the hole for that printf function is actually imported and linked with our hello dot o file and this finally creates our executable file hello which we can finally execute or our command prompt or terminal.

What happens when we run this file?

The computer's architecture looks something like this there is your hard disk where your files are stored including our executable hello file and then there is the main memory and also obviously there is a CPU that contains the register filearithmetic logical unita bus interface and also the program counter and also to handle the input/output devices there is a USB controller and also a graphics adapter.
For the display all these components are connected with each other by circuits called buses initially our executable file hello is stored on the disk when we run it from our terminal or command prompt the code and the data present in the file hello is copied into the main memory now the program counter holds the first instruction that is to be executed as each instruction is executed by the processor the program counter holds the next instruction to be executed the sum operation between the two numbers is computed by the ALU or in other words 
the arithmetic logical unit a based on the architecture of the computer the data present in the main memory which is related to our program can be copied into the register file which is residing inside the CPU 
So that the processor can access it at a much faster rate as compared to accessing it from the main memory and finally when the machine instruction that is related to the printf function in our program gets executed by the processor the output or in other words the sum of these two numbers is actually sent to the display device and there we go we have the output displayed on our screen.

So yeah this is a general idea of how a program is understood and exit by a computer all right so that's it for languages like Java convert the source code into an intermediate form known as the bytecode instead of directly converting them into machine instructions so when you're running the compiled Java file which is a dot class file that is basically bytecode a program called a java virtual machine then converts this bytecode into machine instructions this feature allows portability which means the same compiled Java program can run on any computer of any architecture this feature of probably lacks in C language because if you're using C language to write code we need to create different executable files for computers with different architectures.