Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
You begin a macro definition with the # symbol followed by the preprocessor directive and then any parameters to the directive such as the name of the macro and so on. Table 9.1 shows a listing of the most commonly used preprocessor directives.
| Directive | Purpose |
|---|---|
| #define | Used to define new macros such as constants and functions. |
| #ifdef | Begins an optional compilation block. If the parameter for the preprocessor directive is defined to be anything (even zero), then the code following the #ifdef up until a terminating #endif, #else, or #elif will be compiled and included in the application. If the parameter is not defined and an #else or #elsif block is provided, then the #else or #elsif block will be evaluated and if appropriate compiled and included in the application. |
| #undef | Removes a previously defined macro. |
| #import | Reads and includes another source file in this file. Guards against including the file multiple times automatically. |
| #include | Reads and includes another source file in this file. Does not prevent including a file multiple times. |
| #pragma | Special macro used for configuring the compiler and for annotations in the IDE. |
| #warning | Generates a compiler warning. Used to flag issues to the developer. |
| #error | Generates a compiler error. |
| #if | Begins a conditional compilation block similar to #ifdef, but relies on an expression (such as X > 10) which must evaluate to true in order to be considered true. |
| #else | Used after an #if or #ifdef to provide a conditional block to be compiled if the statement is false. |
| #elif | Used after an #if or #ifdef to provide an additional conditional block with an additional control statement to determine if it should be compiled. |
| #endif | Terminates an #if, #ifdef, #else, or #elif block. |