Summary: Conditional directives are useful for making different versions of a program.
Conditional directives can make QuickC skip part of a source file. They are used primarily to create different versions of a program. While developing a program, for instance, you might want to include debugging code at some times but not others. Or, if you plan to move a program to some other machine, you can compile machine-specific sections of code only for a certain machine.
The C-language conditional directives are listed below.
#if #else #elif | #endif #ifdef #ifndef |
NOTE :
The #ifdef and #ifndef directives are obsolete under the ANSI C standard; see “The defined Operator” below.
Summary: The #if directive works like the if statement.
The #if and #endif directives work like an if statement, allowing you to compile a block of source code if a given condition is true. The #if directive is followed by a constant expression, which the compiler tests at compile time. If the expression is false, the compiler skips every line between the #if and the next #endif.
The example below calls the display function only if the name DEBUG was previously defined as 1 (with #define).
#if DEBUG == 1
display( debuginfo );
#endif
Here, the “conditional block” is a single line (the display function call). A conditional block can contain any number of valid C program lines, including preprocessor directives as well as executable statements.
The test expression for a conditional directive can be almost any expression that evaluates to a constant, with a few minor exceptions (the expression can't use the sizeof operator, type casts, or the float and enum types).
Summary: The #else directive works like the else keyword.
The #else and #elif directives work like the else keyword and can perform more complex conditional tests. For example, you could use code like that in the following example to build different versions of a program for various IBM PC computers, including different files for each computer.
#if XT == 1
#include “XT.H”
#elif AT == 1
#include “AT.H”
#else
#include “PS2.H”
#endif
The code includes the file XT.H if the name XT is defined as 1 and it includes the file AT.H if the name AT is defined as 1. If both XT and AT are undefined, the third conditional block executes, causing QuickC to include the file PS2.H.
You can nest conditional directives in the same way as you would conditional
C language statements.