xHarbour Reference Documentation > Preprocessor Reference xHarbour Developers Network  

#ifdef

Compiles a section of code depending on the presence of a #define constant.

Syntax

#ifdef <constant>
  <statements1>
[ #else
  <statements2> ]
#endif

Arguments

<constant>
This is the symbolic name of a #define constant.
<statements1> .. <statements2>
These are program statements to include or exclude in the preprocessor output. <statements1> is included when <constant> is defined. Otherwise, the statements following #else, if present, are included.

Description

The #ifdef..#else..#endif directives are used for conditional compilation. They work similar to the IF..ELSE..ENDIF statements, but are resolved by the preprocessor, rather than the compiler. #ifdef tests if a #define constant exists. When the constant is defined, the statements following #ifdef are included in the preprocessor output. When the constant does not exist, the statements folowing #else, if present, are used. #endif closes the conditional compilation block.

Info

See also:#define, #if, #ifndef, #undef
Category: Preprocessor directives

Example

// The example demonstrates how to implement an extended error
// checking in the DEBUG version of a program. When DEBUG is not
// defined, all ErrCheck() pseudo-functions are removed from
// the PRG code by the preprocessor.

   #define DEBUG

   #ifdef DEBUG
      #xtranslate ErrCheck(<errCond>,<msg>) => ;
         IF (<errCond>) ;;
            IF Alert( "Error:;"+<(msg)>,{"Continue","Ouit"}) <> 1 ;;
               QUIT ;;
            ENDIF ;;
         ENDIF
   #else
      #xtranslate ErrCheck(<errCond>,<msg>) =>
   #endif

   PROCEDURE Main( cParams )
      ErrCheck( Empty(cParams), "Command line params missing" )

      ? "Error check OK"
   RETURN

Copyright © 2006-2007 xHarbour.com Inc. All rights reserved.
http://www.xHarbour.com
Created by docmaker.exe