cpp: Conditionals

1 
1 4 Conditionals
1 **************
1 
1 A "conditional" is a directive that instructs the preprocessor to select
1 whether or not to include a chunk of code in the final token stream
1 passed to the compiler.  Preprocessor conditionals can test arithmetic
1 expressions, or whether a name is defined as a macro, or both
1 simultaneously using the special 'defined' operator.
1 
1    A conditional in the C preprocessor resembles in some ways an 'if'
1 statement in C, but it is important to understand the difference between
1 them.  The condition in an 'if' statement is tested during the execution
1 of your program.  Its purpose is to allow your program to behave
1 differently from run to run, depending on the data it is operating on.
1 The condition in a preprocessing conditional directive is tested when
1 your program is compiled.  Its purpose is to allow different code to be
1 included in the program depending on the situation at the time of
1 compilation.
1 
1    However, the distinction is becoming less clear.  Modern compilers
1 often do test 'if' statements when a program is compiled, if their
1 conditions are known not to vary at run time, and eliminate code which
1 can never be executed.  If you can count on your compiler to do this,
1 you may find that your program is more readable if you use 'if'
1 statements with constant conditions (perhaps determined by macros).  Of
1 course, you can only use this to exclude code, not type definitions or
1 other preprocessing directives, and you can only do it if the code
1 remains syntactically valid when it is not to be used.
1 

Menu