Understanding the Compilation Process in C Programming Language.

George Oburu
2 min readJan 17, 2024

--

Introduction:

The C programming language is known for its efficiency and versatility, making it a preferred choice for system-level programming and the development of various applications. One important feature distinguishing C from other programming languages is its compilation process, which is a significant step in turning human-readable source code into executable binaries. In this article, we will explore the compilation process in C, shedding light on the stages involved and the significance of each step.

Source Code:

The process starts with the creation of C source code files. These files contain the human-readable instructions a programmer writes to implement specific functionality. C source code files usually have a .c extension consisting of functions, declarations, and other necessary elements.

// 0-preprocessor.c

#include <stdio.h>

int main() {

printf(“Hello, Let’s understand the compilation process!\n”);

return 0;

}

Preprocessing:

Before the actual compilation starts, the preprocessor comes into play. The preprocessor handles directives starting with a # token followed by a preprocessor keyword, such as #include, which is used for including header files and performs macro substitutions. The preprocessing output is an expanded source code, normally saved with a .i extension.

bash

gcc -E preprocessor.c -o preprocessor.i

This command runs the preprocessor on preprocessor.c and saves the result in preprocessor.i.

Compilation:

In this stage, preprocessed source code is translated into assembly code. This assembly code targets a specific architecture and is an intermediate step towards generating machine code. The output is an assembly file (.s).

bash

gcc -S preprocessor.i -o preprocessor.s

The -S flag instructs the compiler to stop after generating the assembly code.

Assembly:

In the assembly stage, the computer translates the architecture-specific assembly code into machine or object codes. The output of this stage is an object file (.o).

bash

gcc -c preprocessor.s -o preprocessor.o

The -c flag tells the compiler to stop after generating the object file.

Linking:

This is the final step in the compilation process. This stage combines one or more object files along with any necessary libraries to produce the final executable file. The linker resolves symbols, assigns memory addresses, and creates the executable file.

bash

gcc preprocessor.o -o preprocessor

Here, preprocessor.o is linked to create the executable file named preprocessor.

Execution:

The resulting executable file can now be executed to see the program in action:

bash

./preprocessor

This will output: “Hello, compilation process!”

Conclusion:

Understanding how C programs compile is important for every C programmer. The process from human-readable source code to an executable involves several stages, each with its importance. Navigating through these stages empowers developers to write efficient and portable C code. As you research deeper into C programming, gaining insights into the compilation process will undoubtedly enhance your programming skills and enable you to make stronger and fine-tuned programs.

--

--