Programming in C

(Barry) #1
Conditional Compilation 319

compiles program.cwith the name OSdefined as 2 .This causes the program to be com-
piled to run under Windows.
The special operator


defined (name)


can also be used in #ifstatements.The set of preprocessor statements


#if defined (DEBUG)


#endif


and


#ifdef DEBUG


#endif


do the same thing.The statements


#if defined (WINDOWS) || defined (WINDOWSNT)


define BOOT_DRIVE "C:/"


#else


define BOOT_DRIVE "D:/"


#endif


define BOOT_DRIVEas "C:/"if either WINDOWSor WINDOWSNTis defined and as "D:/"
otherwise.


The #undefStatement


On some occasions, you might need to cause a defined name to become undefined.This
is done with the #undefstatement.To remove the definition of a particular name,you
write


#undef name


So the statement


#undef WINDOWS_NT


removes the definition ofWINDOWS_NT. Subsequent #ifdef WINDOWS_NTor #if defined
(WINDOWS_NT)statements will evaluate as FALSE.
This concludes the discussion of the preprocessor.You have seen how the preproces-
sor can be used to make programs easier to read, write, and modify.You’ve also seen how
you can use include files to group common definitions and declarations together into a
file that can be shared among different files. Some other preprocessor statements that
weren’t described here are described in Appendix A, “C Language Summary.”
In the next chapter, you’ll learn more about data types and type conversions. Before
proceeding, try the following exercises.

Free download pdf