A conditional is a directive that instructs the preprocessor to select whether or not to include a chunk
of code in the final token stream passed to the compiler. Preprocessor conditionals can test arith-
metic expressions, or whether a name is defined as a macro, or both simultaneously using the special
operator.
defined
A conditional in the C preprocessor resembles in some ways an
to understand the difference between them. The condition in an
execution of your program. Its purpose is to allow your program to behave differently from run to
run, depending on the data it is operating on. The condition in a preprocessing conditional directive
is tested when your program is compiled. Its purpose is to allow different code to be included in the
program depending on the situation at the time of compilation.
However, the distinction is becoming less clear. Modern compilers often do test
program is compiled, if their conditions are known not to vary at run time, and eliminate code which
can never be executed. If you can count on your compiler to do this, you may find that your program is
more readable if you use
course, you can only use this to exclude code, not type definitions or other preprocessing directives,
and you can only do it if the code remains syntactically valid when it is not to be used.
GCC version 3 eliminates this kind of never-executed code even when not optimizing. Older versions
did it only when optimizing.
4.1. Conditional Uses
There are three general reasons to use a conditional.
A program may need to use different code depending on the machine or operating system it is to
•
run on. In some cases the code for one operating system may be erroneous on another operating
system; for example, it might refer to data types or constants that do not exist on the other system.
When this happens, it is not enough to avoid executing the invalid code. Its mere presence will
cause the compiler to reject the program. With a preprocessing conditional, the offending code can
be effectively excised from the program when it is not valid.
You may want to be able to compile the same source file into two different programs. One ver-
•
sion might make frequent time-consuming consistency checks on its intermediate data, or print the
values of those data for debugging, and the other not.
A conditional whose condition is always false is one way to exclude code from the program but
•
keep it as a sort of comment for future reference.
Simple programs that do not need system-specific logic or complex debugging hooks generally will
not need to use preprocessing conditionals.
4.2. Conditional Syntax
A conditional in the C preprocessor begins with a conditional directive:
4.2.1. Ifdef
The simplest sort of conditional is
statements with constant conditions (perhaps determined by macros). Of
if
Chapter 4.
Conditionals
statement in C, but it is important
if
statement is tested during the
if
statements when a
if
,
#if
#ifdef
or
.
#ifndef
Need help?
Do you have a question about the ENTERPRISE LINUX 3 - USING CPP and is the answer not in the manual?
Questions and answers