Skip to main content

What is the Preprocessor? (What Happens Before Compilation)

Updated 4 min readMarkdown
Part of seriesC Programming

So far, you’ve been writing C programs like this:

#include <stdio.h>

int main(void)
{
    printf("Hello, world!\n");
    return 0;
}

You write the code → compile → run.

Simple.

But here’s something most beginners don’t realize:

Your code is actually modified before it is compiled

There is a hidden step that happens first.

That step is called:

The Preprocessor

1. Where the Preprocessor Fits

When you compile a C program, it doesn’t go straight to machine code.

There are stages.

.c file → Preprocessor → Compiler → Executable

👉 The preprocessor runs before the compiler even sees your code

2. What is the Preprocessor?

The preprocessor is:

A tool that processes your code before compilation

It looks for special lines that start with:

#

These are called:

Preprocessor directives

3. What Does the Preprocessor Do?

It performs tasks like:

  • including files

  • replacing text

  • removing or adding code

  • preparing your program for compilation

Think of it like:

A smart “find and replace” system that prepares your code

4. The #include Directive

You’ve already used this:

#include <stdio.h>

What it actually does:

It tells the preprocessor:

“Insert the contents of this file here before compiling”

So your code:

#include <stdio.h>

becomes (conceptually):

[contents of stdio.h inserted here]

Two Types of #include

#include <stdio.h>   // system file
#include "main.h"    // your file
Syntax Meaning
<...> look in system directories
"..." look in current directory first

5. The #define Directive

Another very common directive is:

#define SIZE 1024

What does this do?

It tells the preprocessor:

“Replace every occurrence of SIZE with 1024”

Example

#define SIZE 1024

int x = SIZE;

Before compilation, this becomes:

int x = 1024;

6. Important Insight

The preprocessor does text replacement, not calculations

It does not understand C logic.

It simply replaces text.

7. A Simple Example

#include <stdio.h>

#define VALUE 10

int main(void)
{
    int x = VALUE;
    printf("%d\n", x);
    return 0;
}

What the compiler actually sees:

#include <stdio.h>

int main(void)
{
    int x = 10;
    printf("%d\n", x);
    return 0;
}

8. Why the Preprocessor Exists

The preprocessor helps you:

  • reuse code (#include)

  • avoid repeating values (#define)

  • organize programs better

Without it, your programs would be:

  • longer

  • harder to maintain

  • less flexible

9. Common Beginner Confusions

❌ “Is #define a variable?”

No.

#define X 10

👉 This is not stored in memory.

It is just text substitution.

❌ “Does the preprocessor run my code?”

No.

It only prepares the code.

❌ “Can I debug preprocessor code?”

Not directly — because it runs before compilation.

10. Mental Model

Think of the preprocessor like this:

Your code → preprocessor edits it → compiler sees the final version

11. Practice Thinking

Try to reason through these:

  1. If you write:

    #define A 5
    int x = A;
    

    What does the compiler actually see?

  2. What happens when you write:

    #include "main.h"
    
  3. Why is it useful to replace values using #define?

Key Ideas to Remember

  • The preprocessor runs before compilation

  • It processes directives starting with #

  • #include inserts file contents

  • #define replaces text

  • It does not execute code — only modifies it

What’s Next

Now that you understand what the preprocessor does, the next step is:

How macros actually work and how to use them safely

In the next lesson, you’ll learn:

  • function-like macros

  • macro expansion

  • conditional compilation

  • include guards

That’s where things get powerful — and a little tricky.

About the author

  • Heya! 👋 I love helping people, and one of the best ways I do this is by sharing what I learn and build. I started as a pharmacist at a tertiary hospital in Northern Ghana, where I saw how much technology could improve healthcare. That sparked my interest in digital health. I taught myself to code, then joined the ALX Software Engineering program, which became a turning point. I went on to complete a Master of Health Informatics at the University of Toronto. Today I am a software and AI engineer and a health informatician, and I practise remotely as a Digital Health Pharmacist. I combine clinical experience with technical skills to build tools that solve real problems in health and beyond, and I write here about what I build and what I learn.

No comments yet. Be the first to comment.

More from this blog