How C Program Compilation Works
When you press "Run", your C code does not run directly. It goes through 4 steps and becomes a new file that the computer can run.
In this lesson you will see each step, the tool that does it, and the file it creates. You will also see these files yourself with GCC commands.
What You Will Learn
- The 4 steps of compilation, and the loader.
- The file extension created at each step:
.c → .i → .s → .o → .exe. - GCC commands to stop after each step.
- What a linker and a loader do.
Steps of Compilation in C
Exam definition: Compilation in C has four steps: Preprocessing, Compilation, Assembly and Linking. The result is an executable file, which the loader places in memory to run.
| Step | Tool | Input → Output | What happens |
|---|---|---|---|
| 1. Preprocessing | Preprocessor | hello.c → hello.i | Adds header files, replaces macros, removes comments |
| 2. Compilation | Compiler | hello.i → hello.s | Checks errors, converts C into assembly language |
| 3. Assembly | Assembler | hello.s → hello.o | Converts assembly into machine code (object file) |
| 4. Linking | Linker | hello.o → hello.exe | Joins your code with library code (like printf) |
| Then: Loading | Loader | hello.exe → memory | Copies the program into RAM and starts main() |
Memory trick: Steps = P-C-A-L ("Please Compile And Link"). Files = "C I S O E" (.c, .i, .s, .o, .exe).
Object file vs executable file
- Object file (
.o): machine code of your file only. It cannot run yet, becauseprintfis missing. - Executable file (
.exe/a.out): your code + library code joined by the linker. It can run.
GCC Commands for Each Step
gcc -E hello.c -o hello.i (stop after preprocessing)
gcc -S hello.c (stop after compilation → hello.s)
gcc -c hello.c (stop after assembly → hello.o)
gcc hello.o -o hello (linking → hello / hello.exe)
| Option | Stops after | Easy way to remember |
|---|---|---|
-E | Preprocessing | E = Expand (headers and macros) |
-S | Compilation | S = aSsembly code |
-c | Assembly | c = compile only, don't link |
Example 1: See What the Preprocessor Does
Save this as square.c. It has a comment and a macro:
#define SQUARE(x) ((x) * (x))
int main(void)
{
/* find the square of 4 */
int result = SQUARE(4);
return result;
}
Run gcc -E square.c -o square.i and open square.i.
Output (inside square.i, simplified)
int main(void)
{
int result = ((4) * (4));
return result;
}
Code Explanation
#define SQUARE(x) ((x) * (x)): a macro. The preprocessor will replaceSQUARE(something)with((something) * (something)).- In the output, the
#defineline is gone. Its job is done. - The comment
/* find the square of 4 */is gone. Comments are removed. SQUARE(4)has become((4) * (4)). This is called macro expansion.
The real .i file also has a few lines starting with # that show file names and line numbers. We removed them to keep it simple.
How It Works, Step by Step
- Preprocessor: handles every line starting with
#.#include <stdio.h>is replaced by the full text ofstdio.h(hundreds of lines). - Compiler: checks grammar (syntax errors are found here) and translates C into assembly instructions like
movandcall. - Assembler: turns those instructions into binary machine code.
- Linker: finds the machine code of
printfin the C library and joins it with your code. If a function is missing, you get a linker error such asundefined reference. - Loader: when you run the program, the operating system's loader copies it into memory and starts
main().
Example 2: Keep All the In-Between Files
GCC normally deletes the in-between files. The -save-temps option keeps them, so you can see every step. First compile without linking, then link:
gcc -save-temps -c hello.c (creates hello.i, hello.s, hello.o)
gcc hello.o -o hello (linking creates the program)
ls
Output
hello hello.c hello.i hello.o hello.s
On Windows, use dir instead of ls, and you will see hello.exe instead of hello. Open hello.s in an editor to see the assembly code your C program turned into.
Important Points to Remember
- 4 steps: Preprocessing → Compilation → Assembly → Linking.
- Files:
.c→.i→.s→.o→.exe. - Syntax errors are found in the compilation step.
- Undefined reference errors come from the linker.
- The loader loads the executable into memory to run it.
Common Mistakes Students Make
| Wrong idea | Correct idea |
|---|---|
The compiler handles #include | The preprocessor handles #include, before the compiler. |
A .o file can be run | An object file is not complete. It must be linked first. |
| Comments are compiled | Comments are removed by the preprocessor. |
| Linker and loader are the same | The linker creates the executable; the loader runs it. |
Compilation Process Practice Questions
- Write the 4 steps of compilation in C.
- What file extensions are created at each step?
- What does the linker do?
- What is the difference between the linker and the loader?
- Which GCC option stops after preprocessing?
Show answers
- Preprocessing, Compilation, Assembly, Linking.
.i(preprocessed),.s(assembly),.o(object),.exe/a.out(executable).- It joins the object file with library code to create the executable file.
- The linker creates the executable file. The loader copies it into memory and starts it.
-E
Coding Exercise
Write a Hello World program in hello.c. Run gcc -save-temps -c hello.c, then open hello.i and answer: roughly how many lines does it have? Why is it so much bigger than your 6-line program?
Show solution
hello.i usually has several hundred lines (the exact number depends on your system). It is big because the preprocessor copied the whole stdio.h header file into it, which declares printf() and many other functions.
Quick Quiz: Compilation Process in C (MCQ)
-
Which step handles
#includeand#define?A) Linking B) Preprocessing C) Assembly D) Loading
-
What file does the assembler create?
A)
.iB).sC).oD).c -
An "undefined reference" error comes from the:
A) Preprocessor B) Compiler C) Linker D) Loader
-
Which GCC option creates an object file without linking?
A)
-EB)-SC)-cD)-o -
Which tool copies the program into memory to run it?
A) Loader B) Linker C) Assembler D) Editor
Show answers
- B: preprocessing.
- C: the object file
.o. - C: the linker.
- C:
-c. - A: the loader.
Quick Revision for Exams and Interviews
Read this before your exam. The highlighted words are the answer.
Must-remember facts
| 4 steps | Preprocessing → Compilation → Assembly → Linking |
|---|---|
| Files | .c → .i → .s → .o → .exe |
| Preprocessor | Headers, macros, removes comments |
| Compiler | Finds syntax errors, makes assembly |
| Assembler | Makes machine code (object file) |
| Linker | Joins object file + libraries → executable |
| Loader | Loads executable into memory |
| GCC options | -E preprocess, -S assembly, -c object |
Frequently asked questions
- What is an object file?
- The machine code of one source file, not yet linked with libraries, so it cannot run on its own.
- What is the role of the linker?
- It joins object files with library code to make the executable file.
- What is the difference between linker and loader?
- The linker creates the executable; the loader puts it in memory and runs it.
- In which step are syntax errors found?
- In the compilation step.
- What does the preprocessor do with comments?
- It removes them before compilation.
Remember
- Steps: P-C-A-L. Files: C-I-S-O-E.
-E= Expand,-S= aSsembly,-c= compile only.
Next Topic
Next lesson: Types of Errors in C Programming. You will learn the different kinds of errors you will meet, and how to fix each one.