C Programming: The Four Stages of Compilation

Leopoldo Luna
2 min readSep 29, 2020


The C programming language, which was created by Dennis Ritchie, is one of the most used programming languages in the world. It is a very popular programming language for many programmers because it can be applied to many programs without any hindrance.

The journey of a C program can seem pretty simple: the user writes a C program, they use gcc to compile it, and then it gets executed. Seems pretty understandable, right?

Well, have you ever wondered what actually happens under the hood during the process? If you have…or even if you haven’t thought about it…this blog will illustrate the four stages through which the code passes in order to become executable by using the gcc compiler.

To use the gcc one can simply type: gcc filename and the default executable output for gcc is “a.out”. However, the user is able to specify a name, if wanted, for the executable file by using the following syntax:

gcc filename -o outputfile

The compiler also rocks because it displays any errors that might be present in the code and it won’t execute the file until it is fixed. Now that you have some info on gcc…lets move on to the four stages.

1. Preprocessing

The compilation is first started of with the preprocessing stage. In this part of the compilation, the preprocessor stage helps to removes comments and to interpret preprocessor directives. These directives are statements that begin with “#” (i.e. #include). All in all, this stage helps to reduce repetition in the source code.

2. Compiling

The next step is the compilation stage, which allows for the preprocessed code to be translated to assembly instructions. This allows for readable instructions to be produced that the user can understand and easily convert into machine-level language.

The assembly code can be created with: gcc -S filename

3. Assembling

Like mentioned above, a machine code needs to be created after it is translated and this file is known as an object code. The assembling stage is where the machine language instructions get created and then the object file is generated. Basically, the compiler is converting the syntax into machine level code.

The object code can be created with: gcc -c filename

4. Linking

The final stage is linking. This is the part where pieces of the object code get arranged so that it can successfully call on other functions. This stage is also able to link parts from library functions because the C language has a wealth of precomplied libraries that the user can pull codes from! At the end of it all, the links of object files will help create an executable file.

Now you know what happens under the hood before a file becomes executable thanks to our four awesome compiler stages!