Home Segments Index Top Previous Next

634: Mainline

In Chapter 8, you learned that the C compiler allows you to define macro symbols using #define.

The C compiler's preprocessor also allows you to specify that certain lines of code are to be compiled only if a specified macro symbol has been defined. Suppose, for example, that you want certain optional lines of code to be compiled only if you have previously defined VERBOSE to be a macro symbol; otherwise, you want your C compiler to ignore those optional lines entirely.

To arrange for such conditional compilation, you simply sandwich those optional lines of code between a line reading #ifdef VERBOSE and a line reading #endif:

#ifdef VERBOSE 
...             | 
...             | <-- Compile if VERBOSE has been defined 
...             | 
#endif 

Symmetrically, the lines between the compiler instructions #ifndef and #endif are ignored if VERBOSE is defined; otherwise, they are compiled:

#ifndef VERBOSE 
...             | 
...             | <-- Compile if VERBOSE has NOT been defined 
...             | 
#endif