Hi Learners!!
C Compilation Process:
The process of turning your C source code (.c file) into an executable program is called compilation. Unlike interpreted languages (Python, JavaScript), C is a compiled language — which means your code must be translated into machine code before the program can run.
Modern C compilation is usually done in four main phases:
1. Preprocessing Phase
(What happens first – handled by preprocessor)
Main tasks:
- Removes all comments
- Expands all macros (#define, #ifdef, etc.)
- Includes header files (#include <stdio.h>)
- Processes conditional compilation directives (#if, #ifdef, #ifndef, #else, #endif)
- Handles #pragma, #line, etc.
Important fact: After preprocessing → the code is still text, but much bigger and without any preprocessor directives.
File extension convention (not always created physically): .i or .ii (preprocessed source)
2. Compilation Phase (proper)
(Translates preprocessed C code → assembly code)
Main jobs of the compiler:
- Checks syntax (are all brackets, semicolons correct?)
- Checks semantic rules (do variables exist? correct types? etc.)
- Converts C language constructs into low-level assembly instructions
- Does many optimizations (especially in -O2, -O3 modes)
- Produces symbol table (where are variables and functions located?)
Output: Assembly code (human-readable but machine-oriented)
Common file extension: .s
3. Assembly Phase
(Turns assembly code into machine code – object code)
The assembler does:
- Translates mnemonics (mov, call, push...) into actual binary machine instructions
- Resolves local labels
- Creates relocatable object file
Output: Object file (.o or .obj)
This file contains:
- Machine code
- Symbol table (which functions/variables are defined here, which are needed from outside)
- Relocation information (addresses that will be fixed later)
4. Linking Phase
(The final and very important step)
The linker does:
- Takes one or more object files (.o)
- Combines them with libraries (like libc, libm, etc.)
- Resolves external references (where is printf actually located?)
- Performs relocation (fixes all addresses)
- Produces final executable file
| Stage | Tool | Input | Output | Main job | Still text? |
|---|---|---|---|---|---|
| Preprocessing | Preprocessor | .c | .i | Macro expansion, #include | Yes |
| Compilation | Compiler | .i | .s | Syntax/semantic check, code generation | No (asm) |
| Assembly | Assembler | .s | .o / .obj | Machine code generation | No |
| Linking | Linker | .o + libs | executable | Combine files, resolve symbols | No |