Hello, readers! In this article, we will be focusing on Behind the screen journey of Compilation of a C Program in detail.
So, let us get started!
First, what is compilation?
Compilation is an automated process of conversion of a source code (script) into an object code.
In software terms, a Compiler compiles a script i.e. it converts the source code into object code. But, before the conversion, it checks for the presence of any syntax errors in the code, after which the code is converted into an object code.
Let us now have a look at the various phases of compilation.
Phases in Compilation of a C Program
While Compiling a C program, it undergoes the following phases of execution:
In the below section, we will be having a look at each phase of compilation in detail.
Initially, when we execute a simple C script/code, the source code with the extension .c gets passed to the preprocessor which does all the error checks and transforms it using macros that are predefined.
Further, it does process the source code, expands it, and generates an output that is further fed to the compiler.
In the compilation phase, the compiler takes the output file from the preprocessor and then converts it into an assembly-level code. Thus, the high-level language gets converted into a low-level language code which can be further processed to be fed to the machine level processing
The output from the compiler is fed to the assembler.
In this phase, the assembler converts the assembly-level code into an object file.
Here, the name of the object file is the same as that of the source file but what differs is the extension. The object file would carry ‘.obj’ as the extension for it.
We often use predefined libraries in our script to have ease of work while coding. The object files of these predefined libraries are a store with a ‘.lib’ extension. The main objective of a Linker is to link or combine the object code returned by the assembler with the object codes of the libraries.
As a result, the object file gets converted into a file with the extension as ‘.exe’. This file is further sent to the loader for final execution.
The task of the loader is to load the obtained executable file of the source code into the environment for its execution and output generation.
The Story Behind The Compilation of a C Program!
Let us have a look at the below example!
printf("Welcome to the world of JournalDev.");
Now let us start virtually analyzing the compilation process.
- At first, the source code would be checked for errors and would be expanded by the preprocessor. The output is fed to the compiler.
- In the compilation phase, the compiler converts the source code into assembly-level code for the lower level machine operations. The output from the compiler (assembly-level code) is fed to the assembler.
- Further, the assembler would convert the source code ‘sample_app.c’ into the object code as ‘sample_app.obj’.
- After which, the object code is passed to the linker. It will link the object files of the library <stdio.h> to ‘sample_app.obj’ and create an executable file as ‘sample_app.exe’ out of it.
- Finally, the loader loads the sample_app.exe file and performs the execution of the script.
By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question. For more such posts related to C, stay tuned, and till then, happy learning!! 🙂