Examples

The following examples show valid combinations of 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

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

Line Control (#line)

You can cause HP aC++ to set line numbers during compilation from a number specified in a line control directive. The resulting line numbers appear in error message references, but do not alter the line numbers of the actual source code.

Syntax

line-directive ::=

#line digit-sequence [filename]

Description

The #line preprocessing directive causes HP aC++ to treat lines following it in the program as if the name of the source file were filename and the current line number were digit-sequence. This serves to control the file name and line number that are given in diagnostic messages. This feature is used primarily by preprocessor programs that generate C++ code. It enables them to force HP aC++ to produce diagnostic messages with respect to the source code that is input to the preprocessor rather than the C++ source code that is output.

HP aC++ defines two macros that you can use for error diagnostics. The first is __LINE__, an integer constant equal to the value of the current line number. The second is __FILE__, a quoted string literal equal to the name of the input source file. You can change __FILE__ and __LINE__ using #include or #line directives.

Example

#line 5 "myfile"

128 Preprocessing Directives

Page 128
Image 128
HP C/aC++ for PA-RISC Software manual Line Control #line, Example, #line 5 myfile