The Wayback Machine - https://web.archive.org/web/20240615144047/https://www.geeksforgeeks.org/compiling-a-c-program-behind-the-scenes/
Open In App

Compiling a C Program: Behind the Scenes

Last Updated : 14 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The compilation is the process of converting the source code of the C language into machine code. As C is a mid-level language, it needs a compiler to convert it into an executable code so that the program can be run on our machine.

The C program goes through the following phases during compilation:

compilation process in c

Compilation Process in C

How do we compile and run a C program?

We first need a compiler and a code editor to compile and run a C Program. The below example is of an Ubuntu machine with GCC compiler.

Step 1: Creating a C Source File

We first create a C program using an editor and save the file as filename.c

 $ vi filename.c

We can write a simple hello world program and save it.

Step 2: Compiling using GCC compiler

We use the following command in the terminal for compiling our filename.c source file

 $ gcc filename.c –o filename

We can pass many instructions to the GCC compiler to different tasks such as:

  • The option -Wall enables all compiler’s warning messages. This option is recommended to generate better code. 
  • The option -o is used to specify the output file name. If we do not use this option, then an output file with the name a.out is generated.

If there are no errors in our C program, the executable file of the C program will be generated.

Step 3: Executing the program

After compilation executable is generated and we run the generated executable using the below command.

 $ ./filename

The program will be executed and the output will be shown in the terminal.

c file compilation and execution

 

What goes inside the compilation process?

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. This task can be easily verified by using $size filename.o and $size filename. Through these commands, we know how the output file increases from an object file to an executable file. This is because of the extra code that Linker adds to our program. 
 

Note: GCC by default does dynamic linking, so printf() is dynamically linked in above program. Refer this, this and this for more details on static and dynamic linking.


Previous Article
Next Article

Similar Reads

Executing main() in C/C++ - behind the scene
How to write a C program to print "Hello world" without main() function? At first, it seems impractical to execute a program without a main() function because the main() function is the entry point of any program. Let us first understand what happens under the hood while executing a C program in Linux system, how main() is called and how to execute
4 min read
Hello World Program : First program while learning Programming
In this article, I'll show you how to create your first Hello World computer program in various languages. Along with the program, comments are provided to help you better understand the terms and keywords used in theLearning program. Programming can be simplified as follows: Write the program in a text editor and save it with the correct extension
6 min read
C Program to Print the Program Name and All its Arguments
The command-line argument (CLA) is the parameter provided in the system upon request. Command-line conflict is an important concept in system C. It is widely used when one needs to control your system from the outside. Command-line arguments are transferred to the main () path. Argc calculates the number of arguments in the command line and argv []
2 min read
C Program for Program to Interchange Diagonals of Matrix
Write a C program for a given square matrix of order n*n, the task is to interchange the elements of both diagonals. Examples : Input : matrix[][] = {1, 2, 3, 4, 5, 6, 7, 8, 9} Output : matrix[][] = {3, 2, 1, 4, 5, 6, 9, 8, 7} Input : matrix[][] = {4, 2, 3, 1, 5, 7, 6, 8, 9, 11, 10, 12, 16, 14, 15, 13} Output : matrix[][] = {1, 2, 3, 4, 5, 6, 7, 8,
2 min read
C program to detect tokens in a C program
As it is known that Lexical Analysis is the first phase of compiler also known as scanner. It converts the input program into a sequence of Tokens. A C program consists of various tokens and a token is either a keyword, an identifier, a constant, a string literal, or a symbol.For Example: 1) Keywords: Examples- for, while, if etc. 2) Identifier Exa
5 min read
C program to print a string without any quote (single or double) in the program
Print a string without using quotes anywhere in the program using C or C++. Note : should not read input from the console. The idea is to use macro processor in C (Refer point 6 of this article). A token passed to macro can be converted to a string literal by using # before it. C/C++ Code // C program to print a string without // quote in the progr
1 min read
Output of the program | Dereference, Reference, Dereference, Reference....
Predict the output of below program #include&lt;stdio.h&gt; int main() { char *ptr = &quot;geeksforgeeks&quot;; printf(&quot;%c\n&quot;, *&amp;*&amp;*ptr); getchar(); return 0; } Output: g Explanation: The operator * is used for dereferencing and the operator &amp; is used to get the address. These operators cancel effect of each other when used on
1 min read
Quine - A self-reproducing program
A quine is a program which prints a copy of its own as the only output. A quine takes no input. Quines are named after the American mathematician and logician Willard Van Orman Quine (1908–2000). The interesting thing is you are not allowed to use open and then print file of the program. To the best of our knowledge, below is the shortest quine in
1 min read
C Program to list all files and sub-directories in a directory
#include &lt;stdio.h&gt; #include &lt;dirent.h&gt; int main(void) { struct dirent *de; // Pointer for directory entry // opendir() returns a pointer of DIR type. DIR *dr = opendir(&quot;.&quot;); if (dr == NULL) // opendir returns NULL if couldn't open directory { printf(&quot;Could not open current directory&quot; ); return 0; } // Refer http://pu
1 min read
C Program to print contents of file
fopen() is used to open and fclose() is used to close a file in C #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; // For exit() int main() { FILE *fptr; char filename[100], c; printf(&quot;Enter the filename to open \n&quot;); scanf(&quot;%s&quot;, filename); // Open file fptr = fopen(filename, &quot;r&quot;); if (fptr == NULL) { printf(&quot;Ca
1 min read