C languageC SolutionChapter 8Programming

Q6. Compiler Behind the scene. How does compilation work. How many phases are there in Compilations? PreProcessor, Compiler, Assembler, Linker and Loader.

Program: 150

Compilation:

The Compilation is a process of converting the source code or high level code into object code or machine code (binary 0 and 1) so that the program can run on machine.

There are four phases for a C program to become an executable:

  1. Pre-Processing
  2. Compiling
  3. Assembly
  4. Linking

1. Pre-Processing (Pre-Processor):

Input: file.c
Output: file.i

First phase of compilation. It checks the source code syntax and structure, if source code is error-free, then it perform the four basic functionality given below:

  • Comments Removal
  • Expansion of MACRO
  • Expansion of Included Files
  • Conditional Compilation

2. Compiling (Compiler):

Input: file.i
Output: file.s

Second phase of compilation. It converts expanded code into the intermediate compiled code having assembly level instructions (assembly code).

3. Assembly (Assembler):

Input: file.s
Output: file.obj (DOS) or file.o (UNIX)

Third phase of compilation. It converts the assembly code into object code by using assembler.

Extension of output file depends upon operating system.

For DOS (Disk Operating System), it will create file.obj

For UNIX operating system, it will create file.o

4. Linking (Linker):

Input: file.obj (DOS) or file.o (UNIX)
Output: file.out or file.exe

Fourth phase of compilation. It converts object code into machine code by using linker. It combines the object code of library files with object code of our program.

For example: If we have printf() function in our program then it will add its associated code into our program object code.

Extension of output file depends upon operating system.

For DOS (Disk Operating System), it will create file.exe

For UNIX operating system, it will create file.out

5. Loading (Loader):

The loader loads the executable file for the execution.

Lokesh Kumar

Being EASTER SCIENCE's founder, Lokesh Kumar wants to share his knowledge and ideas. His motive is "We assist you to choose the best", He believes in different thinking.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.