Next: Exercises Up: The C Preprocessor Previous: #include

#if - Conditional inclusion

#if evaluates a constant integer expression. You always need a #endif to delimit end of statement.

We can have else etc. as well by using #else and #elif - else if.

Another common use of #if is with:

#ifdef
- if defined and
#ifndef
- if not defined

These are useful for checking if macros are set - perhaps from different program modules and header files.

For example, to set integer size for a portable C program between TurboC (on MSDOS) and Unix (or other) Operating systems. Recall that TurboC uses 16 bits/integer and UNIX 32 bits/integer.

Assume that if TurboC is running a macro TURBOC will be defined. So we just need to check for this:

As another example if running program on MSDOS machine we want to include file msdos.h otherwise a default.h file. A macro SYSTEM is set (by OS) to type of system so check for this:


Dave.Marshall@cm.cf.ac.uk
Wed Sep 14 10:06:31 BST 1994