Compilation Process in C language
What is the Compilation Process?
The compilation is a method whereby the source code is converted into object code. It is achieved with compiler assistance. The compiler tests the source code for syntactic or structural errors and produces the object code if the source code is error-free.
In object code or machine code, the compilation method c converts the source code that has been taken as an input. The method of compiling can be divided into four stages, i.e., pre-processing, compilation, assembly, and linking.
A pre-processor selects the raw data as an input and extracts all the statements from the code. The pre-processor takes in and interprets the pre-processor instruction. For example, if the directive is accessible in the program <stdio.h>, then the pre-processor interprets the directive and replaces it with the content of the file 'stdio.h.'
Following are the steps that a program goes through until it is translated into an executable form:
- Preprocessor
- Compiler
- Assembler
- Linker
![Compilation Process in C language](https://www.tutorialandexample.com/wp-content/uploads/2020/05/Compilation-Process-in-C-1.png)
Preprocessor
Source code is the code written in a text editor, and an extension is provided to the source code file ".c ». This source code is first transferred to the preprocessor, and then that code is extended by the preprocessor. The extended code is transferred to the compiler after the extension of the code.
Compiler
The code expanded by the preprocessor is passed to the compiler. The code is translated to assembly code by the compiler. Or we can say that the C compiler transforms the preprocessed code into assembler.
Assembler
With the help of an assembler, the assembly code is translated to object code. The name of the assembler generated object file is similar to that of the source file. The object file extension in DOS is ‘obj’, and the filename is 'o' in Unix. If the source file name is sample.c, the object file name would be 'sample.obj.'
Linker
All C-written programs primarily use library functions. These library functions are pre-compiled, and the library files object code is stored with the .lib or .a extension. The linker's main function is to fuse the object code from library files with the object code of our program. Often the situation occurs when the functions specified in other files are referred to by our program; then linker plays a very important role here. It connects those files' object code to our software.
So we infer that the linker's role is to link our program's object code to the library files' object code and other programs. The linker's output is the executable script. The executable file name is similar to the source file, which varies only in its extensions. In DOS, the executable file extension is '.exe', and in UNIX, the executable file can be renamed 'a.out.' For example, if we use the function printf) (in a program, the linker will add its associated code to an output file.
We try to explain you with the help of an example.
#include <stdio.h> int main() { printf("Hello Tutorialandexample"); return 0; }
Output:
![Compilation Process in C language](https://www.tutorialandexample.com/wp-content/uploads/2020/05/Compilation-Process-in-C-2.png)
In the above flow diagram, the following steps are taken to execute a program:
![Compilation Process in C language](https://www.tutorialandexample.com/wp-content/uploads/2020/05/Compilation-Process-in-C-3.png)
- First, the input file is transferred to the preprocessor, i.e., hello.c, and then the preprocessor transforms the source code to extended source code. The expanded source code extension will be hello.i.
- The expanded source code is transferred to the compiler, and this expanded source code is converted to assembly code by the compiler. The assembly code extension will be a hello.s.
- It then sends this assembly code to the assembler, which transforms the assembly code into object code.
- When an object code has been developed, the linker generates the executable file.