Sams Teach Yourself C in 21 Days

(singke) #1
8: /* Header file information goes here... */
9:
10:
11:
12: #endif

Examine what this header file does. On line 3, it checks whether prog_his
defined. Notice that prog_his similar to the name of the header file. If prog_his
defined, a comment is included on line 4, and the program looks for the #endifat the
end of the header file. This means that nothing more is done.
How does prog_hget defined? It is defined on line 6. The first time this header is
included, the preprocessor checks whether prog_his defined. It won’t be, so control goes
to the #elsestatement. The first thing done after the #elseis to define prog_hso that
any other inclusions of this file skip the body of the file. Lines 7 through 11 can contain
any number of commands or declarations.

610 Day 21

LISTING21.5 continued

ANALYSIS

You should include preprocessor directive checks as shown in listing 21.5
with all of the header files you create. This will prevent them from being
included multiple times.

Tip


The#undefDirective ....................................................................................

The#undefdirective is the opposite of #define—it removes the definition from a name.
Here’s an example:
#define DEBUG 1
/* In this section of the program, occurrences of DEBUG */
/* are replaced with 1, and the expression defined( DEBUG ) */
/* evaluates to TRUE. *.
#undef DEBUG
/* In this section of the program, occurrences of DEBUG */
/* are not replaced, and the expression defined( DEBUG ) */
/* evaluates to FALSE. */
You can use #undefand#defineto create a name that is defined only in parts of your
source code. You can use this in combination with the #ifdirective, as explained earlier,
for more control over conditional compilations.

33 448201x-CH21 8/13/02 11:16 AM Page 610

Free download pdf