The preprocessor is the step that runs before the compiler reads your code. It handles the lines that start with #, such as #include and #define, and it rewrites the source text it is given. C and C++ both use one, and the rules are almost the same in both languages.
What the preprocessor does
Before the compiler translates a file, the preprocessor scans it and makes changes such as these:
- It inserts the text of header files (
#include). - It replaces names with values or code (
#define). - It keeps or drops blocks of code based on conditions (
#if,#ifdef,#ifndef,#endif). - It reports errors or passes options (
#error,#pragma).
The preprocessor works on text and tokens. It does not check types, and it does not know about functions or variables in the usual sense.
#include: pulling in header text
#include <stdio.h>
The preprocessor replaces this line with the contents of stdio.h, so the compiler sees those declarations as if you had typed them in the file. Angle brackets make the preprocessor search the system include paths. Quotes usually search the folder of the current file first.
#define: constants and macros
#include <stdio.h>
#define MAX_USERS 100
int main(void) {
printf("Limit: %d\n", MAX_USERS);
return 0;
}
Before compiling, every MAX_USERS becomes 100. A macro with no parameters works like a named constant, but it has no type. Macro names are usually written in uppercase so they stand out in the code.
Conditional compilation and header guards
#ifdef and #ifndef test whether a name has been defined. Common uses include debug output and code that only some platforms need.
#ifdef DEBUG
printf("debug value: %d\n", value);
#endif
You can define the name from the command line with -DDEBUG, as in gcc -DDEBUG main.c.
Conditionals are also used for a header guard in c, which keeps a header from being processed twice in the same file:
#ifndef GREETING_H
#define GREETING_H
void greet(void);
#endif
Without the guard, including the header twice would repeat the declarations and can cause errors.
Macros are text, so watch the parentheses
A function-like macro looks like a function call, but the preprocessor only substitutes text:
#define SQUARE(x) x * x
int y = SQUARE(1 + 2); /* becomes 1 + 2 * 1 + 2, which is 5 */
Adding parentheses around each use of the parameter fixes the problem:
#define SQUARE(x) ((x) * (x))
Now SQUARE(1 + 2) becomes ((1 + 2) * (1 + 2)), which is 9. Macros also ignore types and scope, so they can hide mistakes that a compiler would normally catch. The reasons C++ code avoids them are covered in why not use macros in C++.
What C++ uses instead
C++ offers tools that do many of the jobs macros do, with type checks and scope rules. A constexpr variable is a typed constant:
constexpr int max_users = 100;
An inline function replaces a function-like macro without the parentheses problem:
inline int square(int x) {
return x * x;
}
Calling square(1 + 2) evaluates 1 + 2 once and returns 9. For compile-time work on types and values, C++ uses templates, which are covered in c metaprogramming.
Seeing the preprocessed output
To see exactly what the preprocessor produced, stop the build after that step with -E:
gcc -E main.c
g++ -E main.cpp
The output shows the full text the compiler receives, with headers expanded and macros replaced. It is a useful way to check a macro that does not behave as expected.
The preprocessor is sometimes described as a macro processor, and the general idea is explained in what is a macroprocessor. For how to write macros in practice, see what are macros in C code.