Skip to main content

Command Palette

Search for a command to run...

What is the Preprocessor? (What Happens Before Compilation)

Published
4 min read
D

Heya! 👋 I love helping people, and one of the best ways I do this is by sharing my knowledge and experiences. My journey reflects the power of growth and transformation, and I’m here to document and share it with you.

I started as a pharmacist, practicing at a tertiary hospital in the Northern Region of Ghana. There, I saw firsthand the challenges in healthcare delivery and became fascinated by how technology could offer solutions. This sparked my interest in digital health, a field I believe holds the key to revolutionizing healthcare.

Determined to contribute, I taught myself programming, mastering tools like HTML, CSS, JavaScript, React, PHP, and more. But I craved deeper knowledge and practical experience. That’s when I joined the ALX Software Engineering program, which became a turning point. Spending over 70 hours a week learning, coding, and collaborating, I transitioned fully into tech.

Today, I am a Software Engineer and Digital Health Solutions Architect, building and contributing to innovative digital health solutions. I combine my healthcare expertise with technical skills to create impactful tools that solve real-world problems in health delivery.

Imposter syndrome has been part of my journey, but I’ve learned to embrace it as a sign of growth. Livestreaming my learning process, receiving feedback, and building in public have been crucial in overcoming self-doubt. Each experience has strengthened my belief in showing up, staying consistent, and growing through challenges.

Through this platform, I document my lessons, challenges, and successes to inspire and guide others—whether you’re transitioning careers, exploring digital health, or diving into software development.

I believe in accountability and the value of shared growth. Your feedback keeps me grounded and motivated to continue this journey. Let’s connect, learn, and grow together! 🚀

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.