Scene behind the compile of a C program

C program compile Process

Recently I was interviewing a candidate, and I was impressed by seeing resume. Candidate mentioned she knows the C well, and I ask her about the basic of C program compile process.

Most of us only have seen the output after the compile of a simple C program, but few people try to get the answer, how it actually happens.

She also did not replied about it. And bypassed the question. I am writing how actually it works, when we compile a C program.
We write a simple C program for example abc.c like :
Now it's time to compile it, we simply follow the basic command to get the output.

Mac Terminal: 

Linux Terminal:

Windows System:
You can use the GitCli tool for it to compile the c program or may use the turbo C compiler.

Stages of the Compile Process

The C program pass through the four phases, then it gives output.
  1. Pre-processing
  2. Compilation
  3. Assembly
  4. Linking
By executing the following command you will get the output, how it works internally to do all these four phases given above.
Mac Terminal:

Linux Terminal:

The generated intermediate files are here in list:

Pre-processing This is the first phase through which source code is passed. This phase include:
  • Removal of Comments
  • Expansion of Macros
  • Expansion of the included files.
  • Conditional compilation
Have a look at intermediate file generated after pre-processing,   abc.i

The next step is to compile  abc.i and produce an; intermediate compiled output file  abc.s

Assembly :
In this phase the  abc.s is taken as input and turned into  abc.o by assembler. This file contain machine level instructions. The executable instructions.

This is the final phase in which all the linking of function calls with their definitions are done. Linker knows where all these functions are implemented.It adds some extra code to our program which is required when the program starts and ends.

Hope this helpful!!!

Scene behind the compile of a C program
BitHost Private Limited May 26, 2022
Share this post
Our blogs
Sign in to leave a comment
Deploying a Laravel application to Elastic Beanstalk