Table of Contents
Compiling A C Program: Behind The Scenes
Compiling a C program is a fundamental process in software development that transforms human-readable C code into machine-executable instructions. While many programmers rely on this process daily, few will explore the complexity of what happens behind the scenes. Understanding the inner workings of C program compilation can provide valuable insights into how your code is translated into a format that a computer can understand and execute efficiently. In this exploration, we will focus on a journey to uncover the mysteries behind C program compilation, shedding light on the steps and transformations that occur from your code editor to the running program on your computer.
How do we compile and run a C program?
To compile and run a C program, you’ll require two essential tools: a compiler and a code editor. In this example, we’ll focus on using the GCC compiler on an Ubuntu machine.
Step 1: Creating a C Source File
The initial step is to craft a C program using a code editor of your choice and then save the file with a “.c” extension, like “filename.c”.
$ vi filename.c
Hence, we can just type Hello World and save it
Step 2: Compiling using GCC compiler
In C program, We will use the command given below in the terminal for compiling our filename.c source file
We use the following command in the terminal for compiling our filename.c source file
We can communicate various instructions to the GCC compiler to perform different tasks, including:
- Using the option
-Wall
to enable all compiler warning messages, which is a good practice for producing higher-quality code. - Employing the option
-o
to specify the name of the output file. If this option isn’t used, the compiler generates an output file named “a.out.”
When there are no errors in our C program, the compiler generates an executable file for the program.
Step 3: Executing the program
Once the compilation is complete, an executable file is generated. To run this executable, you can use the following command
$ ./filename
Hence, the program will be executed, and then the output will be displayed in the terminal
What goes inside the compilation process?
Once the compilation is complete, an executable file is generated. To run this executable, you can use the following command.
- Pre-processing
- Compilation
- Assembly
- Linking
By executing the following command, you can generate all intermediate files in the current directory, in addition to the executable:
$gcc -Wall -save-temps filename.c –o filename
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 in the compilation process involves taking the preprocessed file, filename.i
, and generating an intermediate compiled output file named filename.s
. This file contains the program’s instructions in assembly-level code, which is a lower-level representation of the program.
You can view the contents of the filename.s
file using a text editor or terminal command, such as $nano filename.s
. This assembly-level code is an intermediate step in the compilation process before the final executable is generated. It serves as a bridge between the high-level C code and the machine-level instructions that the computer can execute directly.
3. Assembling
In this phase, the filename.s
file is taken as input and processed by the assembler to produce an filename.o
file. This file contains machine-level instructions, which are instructions that the computer’s CPU can directly understand and execute.
It’s important to note that in this phase, only the existing code in your program is converted into machine language. Function calls, such as printf()
, are not fully resolved at this stage. Instead, they are left as placeholders that will be resolved in a later phase of the compilation process. This allows the compiler to generate code that references external functions without needing to know their exact details at this point. The full resolution of function calls typically occurs during the linking phase when external libraries and functions are connected to your program.
4. Linking
In the final phase of C program compilation, the linker plays a crucial role. It takes all the function calls in your code and connects them to their actual implementations. The linker is aware of where these functions are defined in your program.
Additionally, the linker performs some additional tasks, such as adding extra code to your program. This extra code is essential for setting up the program’s environment, including tasks like handling command-line arguments. When you run commands like $size filename.o
and $size filename
, you can observe how the size of the output file increases from an object file to an executable file. This increase in size is due to the extra code that the linker adds to your program to ensure it runs correctly from start to finish.
So, in essence, the linker not only connects function calls but also ensures that your program has everything it needs to execute correctly, from the beginning to the end of its execution.
FAQ- Compiling A C Program: Behind The Scenes
Q1. What is the compiling process of the C program?
Ans. The compilation process in C serves the purpose of translating human-readable code into machine-readable code while simultaneously checking the syntax and semantics of the code. This process aims to identify any syntax problems or warnings present in our C program, ensuring that it can be executed correctly and efficiently by a computer.
Q2. How to compile a C program in a compiler?
Ans. To write and run a C program in Turbo C IDE:
Open Turbo C IDE.
Create a new file.
Write your C program.
Compile using “Alt + F9.”
Run the program with “Ctrl + F9.”
Q3. Why is compiling important in C programming?
Ans. The compiler’s role is to offer a user-friendly means for humans to give instructions that can be readily transformed into machine code, which microprocessors can understand. It accomplishes this by translating human-readable source code into machine code.
Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together