HPlogo HP C++ Programmer's Guide: HP 9000 Series Workstations and Servers > Chapter 2 The HP C++ Preprocessor

Conditional Compilation

» 

Technical documentation

Complete book in PDF

 » Table of Contents

 » Index

Conditional compilation directives allow you to delimit portions of code that are compiled only if a condition is true.

Syntax

conditional-directive ::=
#if                     constant-expression newline
#ifdef                  identifier newline [group]
#ifndef                 identifier newline [group]
#else                   newline [group]
#elif                   constant-expression newline [group]
#endif
NOTE: #elif is available only with the ANSI C preprocessor.

Here, constant-expression may also contain the defined operator:

defined identifier
defined (identifier)

Description

You can use #if, #ifdef, or #ifndef to mark the beginning of the block of code that will only be compiled conditionally. An #else directive optionally sets aside an alternative group of statements. You mark the end of the block using an #endif directive.

The following #if directive illustrates the structure of conditional compilation:

#if constant-expression
     .
     .
     .

(Code that compiles if the expression evaluates to a nonzero value.)

     .
     .
     .
#else
     .
     .
     .

(Code that compiles if the expression evaluates to zero.)

     .
     .
     .
#endif

The constant-expression is like other C++ integral constant expressions except that all arithmetic is carried out in long int precision. Also, the expressions cannot use the sizeof operator, a cast, an enumeration constant, or a const object.

Using the defined Operator

You can use the defined operator in the #if directive to use expressions that evaluate to 0 or 1 within a preprocessor line. This saves you from using nested preprocessing directives.

The parentheses around the identifier are optional. Below is an example:

#if defined (MAX) & ! defined (MIN)
     .
     .
     .

Without using the defined operator, you would have to include the following two directives to perform the above example:

#ifdef max
#ifndef min

Using the #if Directive

The #if preprocessing directive has the form:

#if constant-expression

Use #if to test an expression. HP C++ evaluates the expression in the directive. If the expression evaluates to a nonzero value (TRUE), the code following the directive is included. Otherwise, the expression evaluates to FALSE and HP C++ ignores the code up to the next #else, #endif, or #elif directive.

All macro identifiers that appear in the constant-expression are replaced by their current replacement lists before the expression is evaluated. All defined expressions are replaced with either 1 or 0 depending on their operands.

The #endif Directive

Whichever directive you use to begin the condition (#if, #ifdef, or #ifndef), you must use #endif to end the if section.

Using the #ifdef and #ifndef Directives

The following preprocessing directives test for a definition:

#ifdef identifier
#ifndef identifier

They behave like the #if directive, but #ifdef is considered true if the identifier was previously defined using a #define directive or the -D option. #ifndef is considered true if the identifier is not yet defined.

Nesting Conditional Compilation Directives

You can nest conditional compilation constructs. Delimit portions of the source program using conditional directives at the same level of nesting, or with a -D option on the command line.

Using the #else Directive

Use the #else directive to specify an alternative section of code to be compiled if the #if, #ifdef, or #ifndef conditions fail. The code after the #else directive is included if the code following any of the #if directives is not included.

Using the #elif Directive

The #elif constant-expression directive, available only with the ANSI C preprocessor, tests whether a condition of the previous #if, #ifdef, or #ifndef was false. #elif has the same syntax as the #if directive and can be used in place of an #else directive to specify an alternative set of conditions.

Examples

The following examples show valid combinations of these conditional compilation directives:

#ifdef SWITCH        // compiled if SWITCH is defined
#else                // compiled if SWITCH is undefined
#endif               // end of if

#if defined(THING)   // compiled if THING is defined
#endif               // end of if

#if A>47             // compiled if A is greater than 47
#else
#if A < 20           // compiled if A is less than 20
#else                // compiled if A is greater than or equal
                     // to 20 and less than or equal to 47
#endif               // end of if, A is less than 20
#endif               // end of if, A is greater than 47

The following are more examples showing conditional compilation directives:

#if (LARGE_MODEL)
#define INT_SIZE 32     // Defined to be 32 bits.
#elif defined (PC) & defined (SMALL_MODEL)
#define INT_SIZE 16     // Otherwise, if PC and SMALL_MODEL
                        // are defined, INT_SIZE is defined 
                        // to be 16 bits.
#endif

#ifdef DEBUG            // If DEBUG is defined, display
cout << "table element : \n";  // the table elements.
for (i=0; i << MAX_TABLE_SIZE; ++i)
     cout << i << " " << table[i] << '\n';
#endif
© Hewlett-Packard Development Company, L.P.