Skip to content
StudyFame

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.

StepToolInput → OutputWhat happens
1. PreprocessingPreprocessorhello.c → hello.iAdds header files, replaces macros, removes comments
2. CompilationCompilerhello.i → hello.sChecks errors, converts C into assembly language
3. AssemblyAssemblerhello.s → hello.oConverts assembly into machine code (object file)
4. LinkingLinkerhello.o → hello.exeJoins your code with library code (like printf)
Then: LoadingLoaderhello.exe → memoryCopies 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, because printf is 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)
OptionStops afterEasy way to remember
-EPreprocessingE = Expand (headers and macros)
-SCompilationS = aSsembly code
-cAssemblyc = 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

  1. #define SQUARE(x) ((x) * (x)): a macro. The preprocessor will replace SQUARE(something) with ((something) * (something)).
  2. In the output, the #define line is gone. Its job is done.
  3. The comment /* find the square of 4 */ is gone. Comments are removed.
  4. 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

  1. Preprocessor: handles every line starting with #. #include <stdio.h> is replaced by the full text of stdio.h (hundreds of lines).
  2. Compiler: checks grammar (syntax errors are found here) and translates C into assembly instructions like mov and call.
  3. Assembler: turns those instructions into binary machine code.
  4. Linker: finds the machine code of printf in the C library and joins it with your code. If a function is missing, you get a linker error such as undefined reference.
  5. 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 ideaCorrect idea
The compiler handles #includeThe preprocessor handles #include, before the compiler.
A .o file can be runAn object file is not complete. It must be linked first.
Comments are compiledComments are removed by the preprocessor.
Linker and loader are the sameThe linker creates the executable; the loader runs it.

Compilation Process Practice Questions

  1. Write the 4 steps of compilation in C.
  2. What file extensions are created at each step?
  3. What does the linker do?
  4. What is the difference between the linker and the loader?
  5. Which GCC option stops after preprocessing?
Show answers
  1. Preprocessing, Compilation, Assembly, Linking.
  2. .i (preprocessed), .s (assembly), .o (object), .exe / a.out (executable).
  3. It joins the object file with library code to create the executable file.
  4. The linker creates the executable file. The loader copies it into memory and starts it.
  5. -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)

  1. Which step handles #include and #define?

    A) Linking   B) Preprocessing   C) Assembly   D) Loading

  2. What file does the assembler create?

    A) .i   B) .s   C) .o   D) .c

  3. An "undefined reference" error comes from the:

    A) Preprocessor   B) Compiler   C) Linker   D) Loader

  4. Which GCC option creates an object file without linking?

    A) -E   B) -S   C) -c   D) -o

  5. Which tool copies the program into memory to run it?

    A) Loader   B) Linker   C) Assembler   D) Editor

Show answers
  1. B: preprocessing.
  2. C: the object file .o.
  3. C: the linker.
  4. C: -c.
  5. A: the loader.

Quick Revision for Exams and Interviews

Read this before your exam. The highlighted words are the answer.

Must-remember facts

4 stepsPreprocessing → Compilation → Assembly → Linking
Files.c → .i → .s → .o → .exe
PreprocessorHeaders, macros, removes comments
CompilerFinds syntax errors, makes assembly
AssemblerMakes machine code (object file)
LinkerJoins object file + libraries → executable
LoaderLoads 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.