C Compilation Process


A C program has an extension .c which is not a runnable program, it’s just the source code written by a programmer that needs to be converted to machine code so that the computer can understand it and run it.

image from tutorend.com

Converting a C program to an executable program file is known as the compilation of a C program.

In this process, many things happen and a C program has to go through these four stages in order to become an executable program.

  1. Preprocessing
  2. Compiling
  3. Linking
  4. Assembler
image from tutorend.com

#1. Pre-Processing

image from tutorend.com

While writing code in C language we save the file with a special extension that is .c for example main.c or sum.c and these files can be called source files and the code inside those can be called source codes.

When a C file is given to a Preprocessor, the Preprocessor scans the whole code and replaces all the directives with their corresponding source codes and values. Basically, every line starting with # is evaluated.

Common directives are #include eg. #include <stdio.h> which is for including source codes from other files in our code in preprocessing actual code from source files is pasted in source source code instead of the #include command before going to the next step in the compilation process.

Macros defined using #define are replaced with their corresponding values.

After all the expansion is done a new file is outputted with a new source code with the extension .i For example, if we had a main.c Then after preprocessing with will get main.i

#2. Compiling

comilation process of c langugae

After our source code is expanded with all header codes values of macros and conditional are valued in the preprocessing phase now .i file comes to the compiling phase.

In the compiling phase, the source code is converted into assembly code and generates a new file with the extension .s which is further passed through the assembler phase.

#3. Assembly

comilation process of c langugae

When .s file is outputted from the compiling phase it gets picked by the assembler and converted into machine-understandable code (binary /hexadecimal code) after this process a new object file is outputted with extension .o or .obj

#4. Linking

comilation process of c langugae

As the name suggests in the linking phase all the library code for functions we used are included in our final executable program.

Sometimes times we may have multiple .c program files for example we may have main. c and sum.c After the assembly phase, we’ll have two different object files main.o and sum.o In this phase, these files are going to be combined into a single executable file and a final file will be generated with the .exe extension.

Executable or .exe files are program files that can run on a computer to perform its intended tasks.

#Conclusion

The compilation process might involve additional steps or variations depending on the development environment and compiler used. Commonly, developers use tools like GCC (GNU Compiler Collection) for compiling C programs which have the above 4 steps.

After all the four stages we get an executable program that we can run on our computer or share with others and they also will be able to run the program easily without needing to compile again.