Code360 powered by Coding Ninjas X Code360 powered by Coding Ninjas X
Last Updated: Mar 27, 2024

Compilation process of a C program

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM


The compilation is the process of transforming source code into object code. It is accomplished with the help of the compiler. The compiler checks the source code for syntactical or structural errors, and if it is error-free, it produces the object code.

Recommended Topic, Sum of Digits in C, C Static Function

Compilation in C

C is a compiled programming language. Its source code is written in the form of a text file using any editor of a programmer's preference, and then it must be compiled into machine code.

The C compilation process converts the input source code into object code or machine code. Pre-processing, Compiling, Assembling, and Linking are the four phases in the compilation process.

C source files are typically named with .c extension. We compile C source files with the command "gcc" (GCC is an abbreviation for GNU Compiler Collection, a compiler system developed by the GNU Project).

Let us now examine what happens when we run gcc main.c and learn about the four key steps of the compilation process.

Before we begin, let's make a .c file and run the program to observe what occurs.

So we made a file called main.c and wrote a C program within it that prints "Hello world!!." We built an executable file a.out after compiling it. We put it to the test, and it did indeed print "Hello world!!". Let's go a little further and wonder what happened and how we ended up with a.out.

Also read, Bit stuffing program in c


This is the very first stage that a source code goes through. The following tasks are completed during this stage:

  • Comment Removal 
  • Macros Expansion
  • Expansion of the files contained
  • Conditional compilation

To further understand preprocessing, we will build the above 'main.c' program with the flag -E, which will print the preprocessed output to standard output. See the example below.

The source file in the above result is filled with a lot of information, but our code is maintained at the end.

To proceed and make things clearer, we will use the following command:

$ gcc -save-temps main.c

The -save-temps flag instructs the compiler to save the gcc compiler's temporary intermediate files in the current directory. So we'll obtain the files main.i, main.s, and main.o, as well as the executable a.out.

Must Read Passing Arrays to Function in C


When the compiler has completed the preprocessing stage, the subsequent phase is to accept the main.i as input, compile it and generate an intermediate compiled result. This stage's output file is called 'main.s'. The assembly level instructions are contained in the output of main.s.

The picture demonstrates that it is written in assembly language, which an assembler can understand.

Also check out - Phases of Compiler and Short int in C Programming


At this point, the main.s file is used as input, and the intermediate file main.o is created. This file, also known as the object file, is created by the assembler, which recognizes and translates a '.s' file containing assembly instructions into a '.o' object file containing machine-level instructions. At this point, only existing code is transformed into machine language; function calls such as printf() remain unresolved.

Because the result of this stage is a machine-level file, it is entirely incomprehensible and will appear as

By looking at this output, we can explain 

Hello World !!= the content to be shown, which we entered in main.c, and the Ubuntu version from which the GCC compiler is running.


This is the final stage in which all function calls are linked to their definitions. The linker is aware of where all of these functions are implemented. Linker also performs some more work, adding some extra code to our application that is necessary when the program starts and stops. For example, there is code that is necessary for environment setup, such as giving command line parameters. Using $size filename.o and $size filename, you can simply validate this task. We can see how the output file grows from an object file to an executable file with these instructions. This is due to the additional code that the linker includes with our software.

You can also read about C dynamic array.

Must Read: Difference Between Compiler and Assembler.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job


  1. Which is the correct order of C compilation steps?
    Preprocessing, Compiling, Assembly, Linking.
  2. What is the difference between compilation and execution in C?
    The compilation process translates source code to machine code, whereas execution indicates that the machine code is ready for processing.
  3. What is meant by the compilation of a program?
    The act of turning programs written in a high-level programming language that is intelligible and authored by humans into a low-level binary code known solely by the computer is referred to as compiling.

Key Takeaways

In this article, we have extensively discussed the Compilation process in a C program. We discussed the four main processes, namely Preprocessing, Compiling, Assembly, and Linking, with examples.

We hope that this blog has helped you enhance your knowledge of the compilation process in C and if you would like to learn more about C, check out our articles C Archives

Recommended Readings:

Do upvote our blog to help other ninjas grow. Happy Coding!

Topics covered
Compilation in C
Key Takeaways