Sams Teach Yourself C in 21 Days

(singke) #1
Because this header file might be included in more than one source file, you want to pre-
vent portions of it from compiling more than once. You can do this by using the pre-
processor directives for conditional compilation (discussed later in this chapter).

External Variables and Modular Programming ............................................

In many cases, the only data communication between the main module and the sec-
ondary module is through arguments passed to and returned from the functions. In this
case, you don’t need to take special steps regarding data visibility; but what about an
external variable that needs to be visible in both modules?
Recall from Day 12, “Understanding Variable Scope,” that an external variable is one
declared outside of any function. An external variable is visible throughout the entire
source code file in which it is declared. However, it is not automatically visible in other
modules. To make it visible, you must declare the variable in each module, using the
externkeyword. For example, if you have an external variable declared in the main
module as
float interest_rate;
you make interest_ratevisible in a secondary module by including the following dec-
laration in that module (outside of any function):
extern float interest_rate;
Theexternkeyword tells the compiler that the original declaration of interest_rate
(the one that set aside storage space for it) is located elsewhere, but that the variable
should be made visible in this module. All externvariables have static duration and are
visible to all functions in the module. Figure 21.4 illustrates the use of the externkey-
word in a multiple-module program.

598 Day 21

If you use externdeclarations and then don’t actually declare the variable
elsewhere, you will get an error. This error will occur either when you link
the final program or at run time.

Caution


In Figure 21.4, the variable xis visible throughout all three modules. In contrast,yis vis-
ible only in the main module and secondary module 1.

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

Free download pdf