LearnContact
Lesson 68 min read

The Compilation Process

When you write a C program, the computer cannot understand it directly. In this lesson, you will learn the complete journey from source code to an executable program and understand the 6 stages of compilation.

Introduction

Computers only understand machine language, which consists of binary instructions (0s and 1s). The C program you write is called source code. Before the computer can execute it, the source code must be translated into machine code. This translation is performed by special software called a compiler.

The complete journey from source code to an executable program is called the Compilation Process.

What is Compilation?

Compilation is the process of converting C source code into machine code so that the computer can understand and execute it. A compiler checks the program for errors and then translates it into an executable file. Without compilation, a C program cannot run.

Why is Compilation Necessary?

Imagine that you write a letter in English. A person who understands only Japanese cannot read it directly. A translator converts the English letter into Japanese.

Similarly, the programmer writes code in C language, but the computer understands only machine language. The compiler acts as the translator.

Real-World Analogy

Think of ordering food at a restaurant. The customer places an order, the waiter takes it to the kitchen, the food is prepared, and the customer receives it. The waiter translates your request into something the kitchen understands.

Programmer
Writes C Program
Compiler (The Waiter)
010101 Machine Code
Computer Executes Program

Overall Compilation Process

The compilation process consists of several stages. Each stage performs a specific task:

Source Code (.c)
1. Preprocessing
2. Compilation
3. Assembly
4. Linking
Executable File → Execution

The 6 Stages of Compilation

Stage 1: Source Code

The first stage is the source code, such as hello.c.

This file contains the C program written by the programmer. At this stage, the computer cannot understand the program.

Stage 2: Preprocessing

The Preprocessor processes all preprocessor directives before actual compilation begins.

During preprocessing, header files are included, macros are expanded, comments are removed, and conditional compilation is processed.

The output is called the expanded source code.

c
#include <stdio.h>
#define PI 3.14
Stage 3: Compilation

During this stage, the compiler checks the program for syntax errors and converts the C source code into Assembly Language.

It verifies keywords, variable declarations, operators, statements, and function calls.

If any errors are found, compilation stops.

Stage 4: Assembly

The Assembly stage converts Assembly Language into Object Code.

Object code is machine-readable but is not yet a complete executable program.

Its extension is usually .obj or .o depending on the operating system.

Stage 5: Linking

Large programs often use predefined library functions such as printf(), scanf(), or sqrt().

The linker connects your object code with the required library files.

After successful linking, the final executable file is created.

Stage 6: Execution

Once the executable file is created, the operating system loads it into memory.

Execution begins from the main() function.

The program performs its tasks and eventually terminates.

Complete Flow Diagram

Programmer
Source Code (.c)
Preprocessor → Compiler → Assembler
Object File → Linker
Executable File → OS → Output

Types of Errors During Compilation

1. Syntax Errors

These occur when the program violates the rules of the C language.

Example: printf("Hello") with a missing semicolon.

2. Semantic Errors

The program follows the correct syntax but performs an incorrect operation.

Example: Using an undeclared variable.

3. Linker Errors

These occur during the linking stage.

Example: Calling a function that has not been defined.

4. Runtime Errors

These errors occur after the program has started executing.

Examples include division by zero, invalid memory access, and file not found.

Runtime errors are not detected during compilation.

Compiler vs Linker

FeatureCompilerLinker
Primary TaskConverts C code into object codeCombines object code with libraries
Error DetectionDetects syntax errorsResolves external references
OutputCreates object files (.o / .obj)Creates executable files (.exe)

Advantages of Compilation

Compilation provides several benefits:

  • Faster program execution
  • Error detection before execution
  • Optimized machine code
  • Better performance and improved security

What Happens if Compilation Fails?

If the compiler finds errors, compilation stops and no executable file is created. The programmer must correct the errors and compile the program again.

Source Code
Compiler Finds Error
Compilation Stops (No Executable)

Common Beginner Mistakes

Forgetting a Semicolon
// Incorrect:
printf("Hello")

// Correct:
printf("Hello");
Missing Header File

Using printf() without including <stdio.h> may produce compiler warnings or errors.

Misspelled Keywords
// Incorrect:
innt main()

// Correct:
int main()
Unmatched Braces or Incorrect Function Names

Every opening brace { must have a corresponding closing brace }. Also, writing print() instead of printf() will result in an error.

Best Practices

  • Save the program before compiling.
  • Read compiler error messages carefully.
  • Fix the first error before addressing others.
  • Use meaningful file names.
  • Keep your compiler updated.
  • Compile frequently while writing code.

Frequently Asked Questions

Does every C program need compilation?

Yes. Every C program must be compiled before execution.

Can I run a .c file directly?

No. The source file must first be compiled into an executable file.

Why are there multiple stages in compilation?

Each stage has a specific responsibility, such as processing header files, checking syntax, generating object code, and linking libraries.

Can one syntax error stop compilation?

Yes. Even a single syntax error can prevent the compiler from generating an executable file.

What is an executable file?

An executable file contains machine code that the operating system can execute directly.

Key Takeaways

  • A computer cannot understand C source code directly.
  • The compiler translates C code into machine code.
  • Compilation consists of preprocessing, compilation, assembly, linking, and execution.
  • Different stages perform different tasks.
  • Errors detected during compilation must be fixed before the program can run.
  • Successful compilation produces an executable file.

Summary

The compilation process is the bridge between the program written by the programmer and the machine code understood by the computer. It involves multiple stages, including preprocessing, compilation, assembly, and linking, each contributing to the creation of an executable program. Understanding this process helps programmers identify errors more effectively.

Next Lesson →

Keywords