C Programming: The Four Stages of Compilation

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

2. Compiling

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

3. Assembling

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

4. Linking

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