When we write a C program, the computer cannot directly execute the C code.
The program goes through several stages before it becomes an executable program.
This entire process is called the compilation process.
What is the Compilation Process?
The compilation process is the series of steps that converts C source code into an executable program that the computer can run.
The main stages are:
Source Code
↓
Preprocessing
↓
Compilation
↓
Assembly
↓
Linking
↓
Executable Program
Let's understand them one by one.
1. Preprocessing
First, the preprocessor handles preprocessor directives such as:
#include <stdio.h>
#define PI 3.14
It processes these directives and produces an expanded version of the source code.
This stage happens before the actual compilation.
2. Compilation
The compiler takes the preprocessed code and translates it into assembly code for the target platform.
During this stage, the compiler also checks the code for many errors, such as syntax and type-related errors.
For example:
int age = ;
would result in a compilation error.
3. Assembly
The assembler converts the generated assembly code into machine code, usually stored in an object file.
The object file contains machine-level code, but it may still have unresolved references to functions or symbols from other files or libraries.
4. Linking
The linker combines object files with required libraries and other object code.
For example:
printf("Hello");
Your program uses printf(), whose implementation is provided by a library.
The linker resolves the required references and produces the final executable.
5. Executable Program
After successful linking, we get an executable program.
The operating system can then load and run it.
The complete process can be visualized as:
C Source File
↓
Preprocessor
↓
Preprocessed Code
↓
Compiler
↓
Assembly Code
↓
Assembler
↓
Object File
↓
Linker + Libraries
↓
Executable
Example
Suppose we write:
#include <stdio.h>
int main()
{
printf("Hello, World!");
return 0;
}
It doesn't directly go from C code to a running program.
Instead:
.c file
↓
Preprocessing
↓
Compilation
↓
Assembly
↓
Linking
↓
Executable
↓
Run
What Happens if There is an Error?
Errors can occur at different stages.
For example:
Preprocessor errors → problems with directives or included files
Compilation errors → syntax or type errors
Linker errors → missing or unresolved functions/symbols
Runtime errors → problems that occur while the program is running
Understanding these stages makes it much easier to understand why a C program fails to build or run.
In Simple Words
The compilation process converts C source code into an executable program through preprocessing, compilation, assembly, and linking.
The easiest flow to remember is:
Preprocess → Compile → Assemble → Link → Run