Preprocessors in C scan the source code before it is compiled and transform it with tools like macros.
Preprocessor commands and macros are included in the source code file and are interpreted before the source code is passed to the compiler.
The preprocessor can modify the final code that is compiled or provide messages to the developer during preprocessing.
Common Uses of Preprocessor Commands
Including Additional Files
One of the most common preprocessor commands is
#include which is used to include the text contents of some other file into the one being compiled.
The preprocessor will replace
#include <stdio.h> with the text contents of the file stdio.h.
The preprocessor includes several statements used for conditional compilation:
#ifdef: shorthand for
#ifndef: shorthand for
#if HELLO > 0printf("Hello world!");#endif
If the macro
HELLO is greater than zero, then the
printf("Hello world!") command will be compiled. If not, then the command will be omitted from the final code being compiled.
#ifdef _WIN32// Compile for 32-bit Windows system#elif defined(_WIN64)// Compile for a 64-bit Windows system#else// Compile for something else#endif
The example above will compile different code for different operating systems based on if the macros
When the preprocessor encounters a macro in the source code, it will replace the macro with the value it has been assigned. Macros are defined by using
#define and can be undefined using
#define PI 3.1416
After this line, every time the preprocessor encounters the macro
PI, it will replace it with 3.1416 in the source code.
After this line, trying to use the macro PI will result in a compilation error.
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.