Detailed Explanation of the C Language Compilation Process

What Is Compilation?

Compilation is the process of converting source code into target code. It is accomplished with the help of a compiler. The compiler checks the source code for syntax or structural errors, and if there are no errors, it generates the target code.

Detailed Explanation of the C Language Compilation Process

The Compilation Process of C Language The compilation process of C language converts the input source code into target code or machine code. The compilation process can be divided into four steps: preprocessing, compilation, assembly, and linking.
The preprocessor receives the source code as input and removes all comments from the source code. The preprocessor also interprets preprocessing directives. For example, if there is a directive like <stdio.h> in the program, the preprocessor will interpret this directive and replace it with the content of the ‘stdio.h’ file.
Before being converted into an executable form, our program goes through the following stages:
  • Preprocessor
  • Compiler
  • Assembler
  • Linker

Detailed Explanation of the C Language Compilation Process

👇 Click to Claim 👇

👉 C Language Knowledge Resource Collection
Preprocessor
The source code is written in a text editor, and the source code file has the extension “.c”. The source code is first passed to the preprocessor, which expands the code. The expanded code is then passed to the compiler.
Compiler
The code expanded by the preprocessor is passed to the compiler. The compiler converts this code into assembly code. Or we can say that the C compiler converts the preprocessed code into assembly code.
Assembler
The assembler converts the assembly code into target code. The target file generated by the assembler has the same name as the source file. In DOS, the target file extension is ‘.obj’, while in UNIX, the extension is ‘o’. If the source file is named ‘hello.c’, then the target file will be named ‘hello.obj’.
Linker
Most programs written in C use library functions. These library functions are precompiled, and the target code of the library files is stored with the extension ‘.lib’ (or ‘.a’). The main job of the linker is to merge the target code of the library files with the target code of our program. Sometimes our program may reference functions defined in other files, and this is where the linker plays a crucial role. It links the target code of these files with our program. Therefore, we can conclude that the job of the linker is to link the target code of our program with the target code of library files and other files. The output of the linker is the executable file. The executable file has the same name as the source file, just with a different extension. In DOS, the executable file extension is ‘.exe’, while in UNIX, the executable file can be named ‘a.out’. For example, if we use the printf() function in our program, the linker will add the associated code to the output file.
#include &lt;stdio.h&gt;
int main() {   printf("Hello, World!");   return 0;}
Now, we will create a flowchart for the above program:

Detailed Explanation of the C Language Compilation Process

The Compilation Process of C Language In the flowchart above, executing the program requires the following steps:
  • First, the input file (i.e., hello.c) is passed to the preprocessor, which converts the source code into expanded source code. The expanded source code will have the extension hello.i.
  • The expanded source code is passed to the compiler, which converts the expanded source code into assembly code. The assembly code will have the extension hello.s.
  • Then, the assembly code is passed to the assembler, which converts the assembly code into target code.
  • After creating the target code, the linker will create the executable file. The loader will load the executable file for execution.
Programmer Technical Exchange Group

Scan the code to join the group and remember to note: city, nickname, and technical direction.



Leave a Comment