Program Compilation
Program Compilation Process Step-By-Step Explanation
Definition Of Program Compilation
The program compilation is defined as the process of translating the high level program into low level machine code in binary using the compiler.
The computer due to its hardware architecture can execute only binary machine instructions. And therefore, all programs written in any language other than the machine code must be first converted to machine instructions.
The program compilation is a multi stage process that converts the human readable high level computer program into low level machine readable code in binary format.
Program Compilation Stages
4 Stages Of Program Compilation
Compilation Stage | Accepts Input | Gives Output | Result |
1. Pre-processing | Program Source Code | Pre-processed Code | Resolve # include Directive Expand # define Directive Remove Comments |
2. Compilation | Pre-processed Code | Assembly Code Instructions | Compiler Produce Assembly Code |
3. Assembly | Assembly Code Instructions | Object Code File | Assembler Produce Object Code |
4. Linking | Object Code Files | Single Machine Code File | Linker Links And Produce Executable Machine Code |
The conversion of program source code into executable file ( dot exe ) happens in four stages. These four stages of compilation are pre-processing , compiler , assembly and finally the linking stage.
However , in most situation all these four stages together is referred as program compilation process . In this tutorial , we are going to discuss all the four stages in details.
Program Compilation Video Tutorial
Program Compilation Process
Table Of Contents
Program Compilation Process
Four Stages Of Program Compilation Explained
The program compilation for a high level programs is a multistage process. The programmer writes the program code as per the programming language syntax.
A compiler is a special type of system software is used to translate the program source code file into machine code. The machine code consist of machine instructions in binary.
The machine instructions are encoded by compiler in specific format called instruction format. The CPU decodes the machine instruction as directed by the instruction format.
The high level computer programs are compiled by the compiler in four stages. Each stage of the compilation process takes the input from previous stage.
Program Compilation
STAGE - 1
Preprocessing
What Is Preprocessing Stage ?
The first stage of the compilation process is called the preprocessor stage. Alternately, this stage is also referred as lexical analysis stage.
The preprocessor stage takes the program source code file as input and provides preprocessed file as output with dot i extension.
The compiler scans the source code file for all preprocessor directives that is all # include and # define codes.
The preprocessing stage includes all the header files , all macros are resolved by replacing them with absolute values and the comments are excluded.
In C programming, the standard library functions are stored into separate header file with dot h extension. The separate header file helps to better organize the program code and improves the readability.
The programmer can include the standard function  header files as well as other files that contains the user defined functions into the program depending upon the functionality of the program.
Program Compilation
STAGE - 2
Compilation
What Is Compilation Stage ?
The compilation is the second stage of the program compilation process. The compiler accepts the preprocessed file as input and provides the assembly code as output file with dot s extension .
The compiler converts all high level program instructions into its equivalent assembly code instructions. These instructions are platform dependent and compiled for a specific architecture.
Program Source Code
Program Assembly Code
Program Compilation
STAGE - 3
Assembling
What Is Assembly Stage ?
The compilation is the second stage of the program compilation process. The compiler accepts the preprocessed file as input and provides the assembly code as output file with dot s extension .
The compiler converts all high level program instructions into its equivalent assembly code instructions. These instructions are platform dependent and compiled for a specific architecture.
Example - Assembly Code
Example - Object Code
Program Compilation
STAGE - 4
Linking
What Is Linking Stage ?
The linking is the final and fourth stage of the compilation process. The main function of the linking is to produce a single executable file ( Sourcefile.exe ) by linking all the object code files .
The large computer programs are organized into number of manageable files. The user defined functions are written in a separate file . These files are tied to the main program file in the header section such as # include in  C language .
Similarly , the programming language also provides standard library functions in the pre-compiled object code format which programmer can readily use to simplify the programming jobs .
The programmer can use  these user defined  functions and standard function libraries  by simply including these files in the beginning of the program header section .Â
Since the standard library code is in the object code form which is a  pre-compiled  format and can be directly included at the linking stage by the linker  while creating an executable file  ( sourcefile.exe for windows platform ) during the program compilation process .
Program Compilation
Why Computer Programs Are Compiled ?
It is important for the students of computer science to understand why do we need to compile the high level programs.
The high level programs are programs are developed in high level programming language such as C language , C ++ , Python , dot Net , and java.
The high level programs are human readable . Each high level programming language has its own syntax and reserve keywords which prompts the computer to perform specific tasks.
The high level programming languages are specially designed keeping in mind the ease of programming. Most high level programming language keywords use common English words.
The programmer can use  these user defined  functions and standard function libraries  by simply including these files in the beginning of the program header section .Â
Since the standard library code is in the object code form which is a  pre-compiled  format and can be directly included at the linking stage by the linker  while creating an executable file  ( sourcefile.exe for windows platform ) during the program compilation process .
Program Compilation
What Is Compiler ?
It is important for the students of computer science to understand why do we need to compile the high level programs.
The high level programs are programs are developed in high level programming language such as C language , C ++ , Python , dot Net , and java.
The high level programs are human readable . Each high level programming language has its own syntax and reserve key words which prompts the computer to perform specific tasks.
The high level programming languages are specially designed keeping in mind the ease of programming. Most high level programming language keywords use common English words.
However , the computer system cannot directly execute the high level program code. At the hardware level , the computer processor can decode and execute only machine instructions in binary.
And therefore , the high level program code written in any programming language must be first converted into low level binary machine code. This conversion process is called as program compilation.
Program Compilation
Join The Best Seller
Computer Science Online Course
This is the most comprehensive and unique Computer Science And Programming Fundamentals course Online which will give you in depth understanding of most important fundamental concepts in computer science And Programming .