C/C++ Programmer's Guide (G06.25+)
Preprocessor Directives and Macros
HP C/C++ Programmer’s Guide for NonStop Servers—429301-002
12-4
#if, #elif, #ifdef, #ifndef, #else, and #endif
Example
This example causes the string following #error to be printed as the error message:
#error This message will be issued by the compiler
#if, #elif, #ifdef, #ifndef, #else, and #endif
The four if directives, together with the #else and #endif directives, define the
bounds of an if section, which conditionally includes or excludes source text. An
if section consists of:
•
An if group, which is one of the four if directives followed by the source text that the
compiler is to include if the controlling condition is true
•
An optional else group, which is the #else directive followed by the source text
that the compiler is to include if the controlling condition is false
•
The #endif directive, which marks the end of the if section
The four if directives offer different controlling conditions:
#if Tests the value of a constant expression. If the value is nonzero, the
source text in the if group is included; otherwise, the source text in an
elif or else group, if present, is included.
#elif Tests the value of a constant expression. If the value is nonzero, the
source text in the elif group is included; otherwise, the source text in
the next elif or else group, if present, is included.
#ifdef Tests the existence of an identifier as a macro name. If the identifier
has been defined (using #define), the source text in the if group is
included; otherwise, the source text in the else group (if present) is
included.
#ifndef Tests the nonexistence of an identifier as a macro name. If the
identifier has not been defined, the source text in the if group is
included; otherwise, the source text in the else group (if present) is
included.
if-section:
if-group
[ else-group ]
#endif newline
if-group: