PATH
Mac OS X Documentation >
The GNU C Preprocessor
Conditionals
In a macro processor, a conditional is a command that allows part of the program to be ignored during compilation, on some conditions. In the C preprocessor, a conditional can test either an arithmetic expression or whether a name is defined as a macro.
A conditional in the C preprocessor resembles an
if
statement in C, but it's important to understand the difference between them. The condition in an
if
statement is tested during the execution of your program. Its purpose is to allow your program to behave differently from run to run, depending on the data it's operating on. The condition in a preprocessor conditional command 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.
There are three reasons to use a conditional:
-
A program may need to use different code depending on the target machine or operating system. In some cases, the code for one operating system may be erroneous on another operating system; for example, it might refer to library routines that don't exist on the other system. When this happens, it isn't enough to avoid executing the invalid code: Merely having it in the program makes it impossible to link the program and run it. With a preprocessor conditional, the offending code can be effectively excised from the program when it isn't valid.
-
You may want to be able to compile the same source file into two different programs. Sometimes the difference between the programs is that one makes frequent time-consuming consistency checks on its intermediate data while the other doesn't.
-
A conditional whose condition is always false is a good way to exclude code from the program but keep it for future reference.
Most programs using only Mac OS X API won't need to use preprocessor conditionals.
The GNU C PreprocessorPrevious | Contents | Next