Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Zortech C++ Language Reference - conditional_directives http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
                          Conditional_Directives
                      #if #elif #endif #ifdef #ifndef                   _

   The  preprocessor  implements  a  number  of  conditional   directives
   including #if expression, which checks if the expression evaluates  to
   zero,  #ifdef  identifier, which checks if an identifier  exists,  and
   #ifndef identifier which does the reverse. Three other directives used
   with  these  conditionals are #else, #elif (else if) and  #endif.  The
   latter  is used to signal the end of the conditional statement.  These
   constructions  may  be  nested.  Conditional  directives  are  use  to
   suppress compilation of parts of a source file by means of a test of a
   constant  expression or identifier which determines which text  blocks
   will be compiled and which will not.

   The #if, #elif, #else and #endif  directives are used together thus:
   #if constant-expression
   //....
   code fragment
   //....
   #elif constant-expression
   //....
   code fragment
   //....
   #elif constant-expression
   #else
   //....
   code fragment
   //....
   #endif

   Each  #if  directive  in a source file must be matched  by  a  closing
   #endif. There is no restriction on the number of #elif directives that
   may  be used between the initial #if and the terminating  #endif,  but
   only  one  #else  directive may be present.  Note  that  the  constant
   expression  used  cannot  include sizeof expressions,  type  casts  or
   enumeration constants.

   The above example is treated by the preprocessor in the following way.
   If  the constant expression following the #if directive is TRUE  (non-
   zero)  then  the  source  code between it  and  the  next  conditional
   directive  is  compiled.  This  process is  repeated  for  each  #elif
   directive  until  the #else is reached. All source  code  between  the
   #else  and  the  terminating #endif is  then  skipped,  including  any
   preprocessor  directives. If the initial expression following the  #if
   directive is FALSE (zero), then all the source code between it and the
   #else  directive  is skipped, and  compilation  continues  immediately
   following  the  #else  directive. If there were  no  #else  directive,
   compilation would continue immediately after the #endif directive.

   Two  modified  versions  of the #if directive  are  supported  by  the
   preprocessor,  #ifdef  and  #ifndef.  These  directives  are  normally
   followed by an identifier. The first, #ifdef, tests for the  existence
   of  the  identifier, whilst #ifndef tests for its  non-existence.  The
   treatment of TRUE and FALSE results by these expressions are the  same
   as that described previously for the #if directive. Note that there is
   an alternative syntax #if defined which can be used instead of #ifdef.

   Other Directives    #line, #error and #pragma.                 _

   There  are  three  miscellaneous  preprocessor  directives  which  are
   supported by C++: #line, #error and #pragma.

   The #line Directive
   The  #line directive is followed by a number and an  identifier.  This
   causes the compiler to change its internally held line number count to
   that  specified by the number parameter and its working name  for  the
   current source file to that given by the identifier. The compiler uses
   this  line  number and file information when  reporting  warnings  and
   errors during compilation. The number used in the #line directive must
   be  a  valid  integer;  the file name can be  any  valid  sequence  of
   characters enclosed in double quotes. If the file name is omitted  the
   original file name is retained.

   The #error directive
   The  directive  #error is used to abort a compilation  with  an  error
   message. The text following it on the same line is output to  standard
   error.

   The #pragma directive
   The  final directive supported is #pragma. This is used  to  implement
   compiler-specific  operations and is followed by a directive  of  some
   sort. Pragmas are often used to turn compilation options on for  small
   sequences  of  code without affecting the bulk of a  compilation.  The
   compiler will silently ignore any directive following a pragma that it
   does not recognize.


Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson