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
#includelines. - It replaces macro names defined with
#definewith their values. - It keeps or drops blocks of code based on
#if,#ifdefand 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.