Execution of Program

There are many steps involved in converting a C program into an executable form.

1.          Pre-processing

2.          Compilation

3.          Assembling

4.          Linking

5.          Loading

The following flowchart shows the flow of the execution

execution-program-c

Pre-processor::

In this step, the C source code is expanded based on the pre-processor directives like #define, #include, #ifdef, etc.

It will eliminate new lines, spaces, comments, and header files from the program.
To know about it follow the below link,
 link:: Pre-processor
The expanded source code is stored in an intermediate file with the ".i" extension.

Hence, if our source code is stored in PR1.C then expanded source code is stored in PR1.I.


Compilation::

The expanded source code is then passed to the compiler, which identifies the syntax errors in the expanded source code.
These errors are displayed along with warnings if any.
If the expanded source code is error-free, the compiler translates the C code into an assembly language.
The assembly code is typically stored in PR1.ASM.


Assembling::

The job of the Assembler is to translate. ASM program into Relocatable Object code.
Here the word ‘Relocatable’ means that the program is complete except for one thing that is no specific memory addresses have yet been assigned to the code.
The relocatable object code gets stored in PR1.OBJ. The .OBJ file created is the specified binary file.
 

Linking::

It is the final stage in creating an executable program.

The Linker combines the data of different ".OBJ" files into a single data section.

It combines the code sections of different ".OBJ" files into a single code Section.
The files which we include in the program such as ‘stdio.h’ are not part of the program so, to combine such different files linker is used.

During linking if the linker detects errors such as miss-spelling, it stops the linking process and doesn’t create the binary executable file. 



 Loading::

Once the .EXE file is created and stored on the disk, it is ready for execution.
When we execute, it is first transferred from the disk to the RAM by program loader, which places the .EXE anywhere in the computer memory depending on the availability.