Compiling is a critical step in converting C source code into executable programs. This process converts the source code into human-readable form. Understanding the compilation process is very helpful for programmers to troubleshoot issues, optimise performance, and gain a deeper appreciation for how C programs are executed on a computer. This article explains the compilation process in C language.
What is a Compilation?
It is the process of converting the source code of the C language into machine source code. As we know, C is the middle-level language. It is performed by a compiler, which reads the entire source code, analyses it for syntax and semantic errors, and translates it into a lower-level language. Then, the resulting executable file can be run on the target machine. It involves sewerage stages, including lexical analysis, syntax analysis, semantic analysis, optimisation and source code generation.
Get curriculum highlights, career paths, industry insights and accelerate your technology journey.
Download brochure
How to Compile and Run a C Program?
You need a compiler and a code editor to compile and run a C program in your computer system.
Step 1: Write Your C program: Create a C source file with .c extension. For example, we can create a file called hello.c with the following source code.
# include <stdio.h>
int main(){
printf("Hello, Worldn") ;
return 0 ;
}
Step 2: Open your command-line interface (Terminal on macOS/ Linux or Command Prompt / PowerShell on Windows).
Step 3: Use the cd command to change the directory where your program is located
Step 4: Run the Program by using the following command.
gcc hello.c -o hello
Step 5: Run the compiled Program using the following command “./hello”
The Compilation Process in C
There are four steps involved in the compilation process.
Preprocessing
Compiling
Assembling
Linking
Pre-Processing
Compilation preprocessing is a crucial initial step in preparing source code for the compilation process. It involves tasks such as file inclusion, where the preprocessor incorporates external files specified by #include, and macro definitions, which are established using #define to create constants or inline functions. The conditional compilation directives like #ifdef allows source code segments to be included or excluded based on specific conditions, aiding in platform-specific adjustments and debugging.
1. Comments Removal
In C language, comments give a general idea about a particular statement or part of the source code. Comments are part of the source code that the preprocessor removes during the compilation process as the machine does not particularly use them. The comments in the below program will be removed when the program is in the preprocessing phase.
/* This is a
multi-line comment in C */
#include<stdio.h>
int main()
{
// this is a single-line comment in C
return 0;
}
2. Macros Expansion
The macros are constant values or expressions defined in the C program using the #define directive. Macros defined in the source code are replaced with their corresponding definitions during the pre-processing phase of compilation. Using shorthand notations for repetitive code patterns or contents allows for more flexible and efficient source code.
More Example
Defining a value
#define G 9.8
3. File Inclusion
File inclusion in C language means adding another file containing some other pre-written source code to our C program during pre-processing. This is done by using the #include directive.
For Example
#include <stdio.h>
4. Compiling
The C compiling phase uses built-in compiler software to convert the intermediate (.i) file into an Assembly file, which has assembly-level instruction (low-level code). The C compiler translates the intermediate file to make an assembly file to boost the programmer’s performance.
5. Assembling
An assembly-level source code (.s file) is converted into an understandable machine source code (in binary/hexadecimal form) using an assembler. Assembler is a pre-written program that translates code into machine source code. It takes basic instructions from an assembly source code file and converts them into binary/hexadecimal source code specific to the machine type.
6. Linking
It is the process of including library files into our program. Some predefined files contain the definition of the function in machine language, and these files have an extension of .lib. The unknown statements are written in the object (.o, .obj) file that our operating system can’t understand.
Example
The following program displays the Hello Word on the output screen.
#include<stdio.h>
int main()
{
printf("Hello World!");
return 0;
}
Output
Hello World
Flow Diagram of the Program
The following diagram is used for the compilation process in C language.
We have a C program file with an extension of .c hello.c file.
In the second step, we have to compile the hello. I file. Compiler software translates the hello.I file it with hello.s with assembly-level instructions (low-level code).
It is assembly-level source code instructions that the assembler converts into machine-understandable source code(binary/hexadecimal form).
A linker links the library files with the object file to define the unknown statements.
The linker is used to link the library files with the object file to define the unknown statements. It generates an executable file with the .exe/ .out extension, hello.exe/hello.out.
Next, we can run the hello.exe/hello.out executable file to get the desired output on our output window “Hello World”.
In this article, we learned about the process in C language. It is a very crucial step that transforms high-level source code into machine-readable source code. This multi-stage process typically includes preprocessing, compiling, assembling, and linking. Each stage serves a specific purpose, such as handling directives and converting source code to an intermediate form, generating object source code, and finally linking various modules and libraries to create an executable.
FAQs
What is linking in C?
Linking is combining one or more object files into a single executable. It resolves references between modules and includes necessary libraries.
How can I optimise the compilation process?
Optimisation can be achieved by compiler flags, organising source code efficiently, reducing dependencies, and minimising includes.
What is an executable file?
An executable file is the final output of the compilation and linking process that can be run on a computer. It contains machine source code that the CPU can execute directly.
What is a semantic error?
A semantic error occurs when the source code is syntactically correct but logically incorrect, leading to unintended behaviour during execution.
Hero Vired is a leading LearnTech company dedicated to offering cutting-edge programs in collaboration with top-tier global institutions. As part of the esteemed Hero Group, we are committed to revolutionizing the skill development landscape in India. Our programs, delivered by industry experts, are designed to empower professionals and students with the skills they need to thrive in today’s competitive job market.