Skip to content
Related Articles

Related Articles

Improve Article

How does a C program executes?

  • Difficulty Level : Easy
  • Last Updated : 21 Dec, 2018

Whenever a C program file is compiled and executed, the compiler generates some files with the same name as that of the C program file but with different extensions. So, what are these files and how are they created?

Below image shows the compilation process with the files created at each step of the compilation process:

Every file that contains a C program must be saved with ‘.c’ extension. This is necessary for the compiler to understand that this is a C program file. Suppose a program file is named, first.c. The file first.c is called the source file which keeps the code of the program. Now, when we compile the file, the C compiler looks for errors. If the C compiler reports no error, then it stores the file as a .obj file of the same name, called the object file. So, here it will create the first.obj. This .obj file is not executable. The process is continued by the Linker which finally gives a .exe file which is executable.

Linker: First of all, let us know that library functions are not a part of any C program but of the C software. Thus, the compiler doesn’t know the operation of any function, whether it be printf or scanf. The definitions of these functions are stored in their respective library which the compiler should be able to link. This is what the Linker does. So, when we write #include, it includes stdio.h library which gives access to Standard Input and Output. The linker links the object files to the library functions and the program becomes a .exe file. Here, first.exe will be created which is in an executable format.

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 and inform the CPU with the starting point of the address where this program is loaded.

CPU Registers

Instruction Register: It holds the current instructions to be executed by the CPU.
Program Counter: It contains the address of the next instructions to be executed by the CPU.
Accumulator: It stores the information related to calculations.

The loader informs Program Counter about the first instruction and initiates the execution. Then onwards, Program Counter handles the task.

Difference between Linker and Loader

Linker generates the executable module of a source program.Loader loads the executable module to the main memory for execution.
Linker takes the object code generated by an assembler, as input.Loader takes executable module generated by a linker as input.
Linker combines all the object modules of a source code to generate an executable module.Loader allocates the addresses to an executable module in main memory for execution.
The types of Linker are Linkage Editor, Dynamic linker.The types of Loader are Absolute loading, Relocatable loading and Dynamic Run-time loading.

Want to learn from the best curated videos and practice problems, check out the C Foundation Course for Basic to Advanced C.

My Personal Notes arrow_drop_up
Recommended Articles
Page :