Compiler control directive
The C Preprocessor offer a feature known as conditional compilation, which can be used to switch on or off a particular line or group of lines in a program.
This is achieved by the inserting #ifdef or #endif.
Conditional selection of code using #ifdef,#endif.
The preprocessor has a conditional statement similar to' C's if else.
It can be used to selectively include statements in a program. This is often used where two different computer types implement a feature in different ways. It allows the programmer to produce a program which will run on either type.
The keywords for conditional selection are; #ifdef, #else and #endif.
#ifdef
takes a name as an argument, and returns true if the name has a current definition. The name may be defined using a #define, the -d option of the compiler.
#else
is optional and ends the block beginning with #ifdef. It is used to create a 2 way optional selection.
#endif
ends the block started by #ifdef or #else.
Where the #ifdef is true, statements between it and a following #else or #endif are included in the program.
Where it is false, and there is a following #else, statements between the #else and the following #endif are included.
This is best illustrated by an example.
Using #ifdef for Different Computer Types
Conditional selection is rarely performed using #defined values. A simple application using machine dependent values is illustrated below:


Note: sun is defined automatically on SUN computers. vax is defined automatically on VAX computers. ibm is defined automatically on IBM pc's else type not defined message will be displayed (different types of computer)
Using #ifdef to Temporarily Remove Program Statements
#ifdef also provides a useful means of temporarily "blanking out" lines of a program.
The lines in question are preceded by #ifdef NEVER and followed by #endif. Of course you should ensure that the name NEVER isn't defined anywhere.

Post a Comment