Node: Ifdef, Next: , Up: Conditional Syntax


The simplest sort of conditional is

     #ifdef MACRO
     controlled text
     #endif /* MACRO */

This block is called a conditional group. controlled text will be included in the output of the preprocessor if and only if MACRO is defined. We say that the conditional succeeds if MACRO is defined, fails if it is not.

The controlled text inside of a conditional can include preprocessing directives. They are executed only if the conditional succeeds. You can nest conditional groups inside other conditional groups, but they must be completely nested. In other words, #endif always matches the nearest #ifdef (or #ifndef, or #if). Also, you cannot start a conditional group in one file and end it in another.

Even if a conditional fails, the controlled text inside it is still run through initial transformations and tokenization. Therefore, it must all be lexically valid C. Normally the only way this matters is that all comments and string literals inside a failing conditional group must still be properly ended.

The comment following the #endif is not required, but it is a good practice if there is a lot of controlled text, because it helps people match the #endif to the corresponding #ifdef. Older programs sometimes put MACRO directly after the #endif without enclosing it in a comment. This is invalid code according to the C standard. GNU CPP accepts it with a warning. It never affects which #ifndef the #endif matches.

Sometimes you wish to use some code if a macro is not defined. You can do this by writing #ifndef instead of #ifdef. One common use of #ifndef is to include code only the first time a header file is included. See Once-Only Headers.

Macro definitions can vary between compilations for several reasons. Here are some samples.