The Preprocessor

Which Is the Preprocessor in C?

The preprocessor is the built-in first step of compiling C, not a language you choose. It handles #include and #define before the compiler reads your code.

On this page
  1. What the preprocessor does
  2. Where it runs in a build
  3. A small example
  4. Is it the same in C++?
  5. Common confusion
  6. Quick summary

The preprocessor is the built-in first step of compiling a C program. It isn’t a separate language or a version you pick. Before the compiler reads your code, the preprocessor handles every line that starts with #, including #include and #define.

What the preprocessor does

The preprocessor works on your source file as text. It makes three kinds of changes before the compiler sees the code:

  • It inserts the contents of files named in #include lines.
  • It replaces macro names defined with #define with their values.
  • It keeps or drops blocks of code based on #if, #ifdef and related directives.

Once those changes are made, the directive lines are gone, and the compiler translates what’s left.

Where it runs in a build

Building a C program usually runs three steps in order: the preprocessor, then the compiler, then the linker. The linker combines the compiled pieces into a finished program. Many toolchains, including GCC’s, ship the preprocessor as a separate program called cpp, and the compiler driver runs it for you. You rarely need to call it yourself, but you can if you want to see its output.

A small example

#include <stdio.h>

#define GREETING "Hello"

int main(void) {
    printf("%s, world\n", GREETING);
    return 0;
}

Before compilation, GREETING is replaced with "Hello", so the compiler sees a call to printf with a plain string. To see this for yourself, run gcc -E hello.c. The output includes the full text of stdio.h, so scroll to the bottom to find the replaced printf line.

Is it the same in C++?

Mostly, yes. C++ inherited most of its preprocessor rules from C, so the directives and macro behavior are largely the same. The main difference is in the headers. C++ programs usually include standard headers such as <iostream> and <vector>, which have no .h ending. C-style headers still work in C++, and <cstdio> is the C++ version of <stdio.h>.

Common confusion

The preprocessor doesn’t know about types, variables or functions. It only sees text and the # lines. That’s why macros can do things a function can’t, such as turning a name into a string, and why they can also cause surprises that ordinary functions avoid.

Quick summary

In C, the preprocessor is the first build step. It includes headers, replaces macros and selects code using lines that begin with #. C++ follows the same preprocessing model, so the same ideas apply.

The complete guide to the preprocessor

What the Preprocessor Does in C++
Read the guide

Keep exploring

Guides from other topics

All topics →

The Latest C++ Standard

C++23 is the most recent C++ standard at the time of writing, with C++26 next. Here is what changed, how versions are named, and how to compile for each.

C++ Standards 3 min read