A compiler converts a C program into an executable. There are four phases for a C program to become an executable: 

  1. Pre-processing
  2. Compilation
  3. Assembly
  4. Linking

By executing the below command, we get all intermediate files in the current directory along with the executable.

 $gcc -Wall -save-temps filename.c –o filename 

The following screenshot shows all generated intermediate files.

intermediate files in c compilation

Intermediate Files

Let us one by one see what these intermediate files contain.

1. Pre-processing

This is the first phase through which source code is passed. This phase includes:

  • Removal of Comments
  • Expansion of Macros
  • Expansion of the included files.
  • Conditional compilation

The preprocessed output is stored in the filename.i. Let’s see what’s inside filename.i: using $vi filename.i 

In the above output, the source file is filled with lots and lots of info, but in the end, our code is preserved. 

  • printf contains now a + b rather than add(a, b) that’s because macros have expanded.
  • Comments are stripped off.
  • #include<stdio.h> is missing instead we see lots of code. So header files have been expanded and included in our source file.

2. Compiling

The next step is to compile filename.i and produce an; intermediate compiled output file filename.s. This file is in assembly-level instructions. Let’s see through this file using $nano filename.s  terminal command.

assembly code from C compiler

Assembly Code File

The snapshot shows that it is in assembly language, which the assembler can understand.

3. Assembling

In this phase the filename.s is taken as input and turned into filename.o by the assembler. This file contains machine-level instructions. At this phase, only existing code is converted into machine language, and the function calls like printf() are not resolved. Let’s view this file using $vi filename.o 

generated binary code in c compilation

Binary Code

4. Linking

This is the final phase in which all the linking of function calls with their definitions is done. Linker knows where all these functions are implemented. Linker does some extra work also, it adds some extra code to our program which is required when the program starts and ends. For example, there is a code that is required for setting up the environment like passing command line arguments.