A macro processor is a program, or a stage of one, that replaces macro names with the text they stand for. In C and C++, that job belongs to the preprocessor. It runs before the compiler, reads lines that start with #, and rewrites the source text.
If you are asking “What is a preprocessor in C?”, the short answer is that it is the macro processor for C and C++. Some sources write the term as one word, macroprocessor, but the meaning is the same.
The general idea
A macro processor takes text in and gives text out. It looks for the names it has been told about, replaces each one with its text, and passes the result on. The idea is not limited to C++. GNU m4 is a general-purpose macro processor, and GNU Autoconf, a tool used to set up software builds, is written with m4 macros.
What the C and C++ preprocessor does
In simple terms, the preprocessor:
- copies in the contents of any file named by
#include - records each
#defineand removes each#undef - keeps or drops blocks marked with
#if,#ifdefor#ifndef, which is how a header guard in C or C++ keeps a header from being processed twice - replaces each macro use with its text, and repeats the replacement when that text contains more macro names
The compiler then receives the resulting text and never sees the macro names. For a learner, that is the practical answer to what does the preprocessor do in C: it rewrites the source before the compiler reads it.
A small example
// hello.cpp
#include <iostream>
#define GREETING "Hello, world"
int main() {
std::cout << GREETING << '\n';
}
To see the preprocessor’s output, run g++ -E hello.cpp. Clang’s clang++ -E works the same way. The output is long, because the contents of <iostream> are copied in. Near the end you will find main, with GREETING already replaced by "Hello, world".
Why this matters for C++ learners
The preprocessor works on text and tokens. It does not know C++ types, scope or namespaces. That is why macros can cause surprises, and it is the core of the answer to “Why not use macros in C++?”
The # operator, which turns a macro argument into a string literal, also happens in this stage. That makes the preprocessor a useful place to look when a macro does something you did not expect.