Preprocessor directive definition
In coding, a preprocessor directive is a special instruction that must be executed before the code is compiled. It is placed at the top of the code, in a separate line beginning with a “#” character. Programmers use preprocessor directives to include or exclude sections of code in certain circumstances, define constants, and perform text substitutions.
Like the name suggests, preprocessor directives are executed by preprocessors — software tools that run before the actual compilation of the code.
See also: compiler
Common preprocessor directive uses
- Specifying parts of the code that should be compiled in the given circumstances. With the right preprocessor directive, you can designate which parts of the code should be read or excluded. For example, #ifdef will instruct the compiler to only include a part of the code if a specific macro has been defined.
- Defining macros. The #define command lets the programmer set a value for a macro. After that, when the compiler encounters these macros in the code, it will automatically replace them with the corresponding value.
- Including the contents of external files in the code. For instance, #include can incorporate header files containing definitions of functions, classes, or constants.
- Controlling compiler behavior. A common example is the #pragma command, which sets out compiler-specific instructions or control warnings.