How a C program executes ?

Being a programmer it is important to know how a program executes and what happens behinds the scenes during compilation and execution. It is also the most common interview question. So today let's learn it in a simple way.

Lets understand using a basic c program

1.Source Code

#include <stdio.h>
 int main()
 {
        printf("Hey there how is it going ");
        return 0;
 }

2.Pre-processing

In this process , all the header files as well as #define expansions used in the program will be replaced by the code (in our case it is #include stdio.h) in our souce code file

After generating the pre-processed code it will be sent to compiler

3.Compilation

Compiler translate the high level language(our souce code) to assmebly language In case of any syntax or semantic errors in the code the it throws an error. The assembly language is sent to assembler from compiler

4.Assembling

Assembly language is a low-level language. it includes all the instructions and operators.Assembly language is something is like this

     ADD -> add two values
     SUB -> subtracting two values
     POP -> pop data from stack and many more

Assembler generates object file which is machine language(binary language) and sends it to linker.

5.Linking

A linker is a computer program that takes collection of object files and command
line arguments and generates fully linked object file that can be runnable and at the end it generates executable file

6.Loader

Whenever we give the command to execute a particular program, the loader comes into work. The loader will load the .exe file in RAM for execution . It allocates the memory space to the executable module in main memory and then transfers control to the beginning instruction of the program

Finally our program gets executed successfully and gives the output. Hope you understand it.