Programming in C

(Barry) #1

336 Chapter 15 Working with Larger Programs


As you’ll see in the last section of this chapter, the process of incremental compilation
can be automated by using a tool called make.The IDE tools that were mentioned at the
beginning of this chapter invariably have this knowledge of what needs recompilation,
and they only recompile files as necessary.

Communication Between Modules


Several methods can be used so that the modules contained in separate files can effec-
tively communicate. If a function from one file needs to call a function contained inside
another file, the function call can be made in the normal fashion, and arguments can be
passed and returned in the usual way. Of course, in the file that calls the function, you
should always make certain to include a prototype declaration so the compiler knows the function’s
argument types and the type of the return value.As noted in Chapter 14, “More on Data
Types,” in the absence of any information about a function, the compiler assumes it
returns an intand converts shortor chararguments to ints and floatarguments to
doubles when the function is called.
It’s important to remember that even though more than one module might be speci-
fied to the compiler at the same time on the command line,the compiler compiles each
module independently.That means that no knowledge about structure definitions, function
return types, or function argument types is shared across module compilations by the
compiler. It’s totally up to you to ensure that the compiler has sufficient information
about such things to correctly compile each module.

External Variables


Functions contained in separate files can communicate through external variables, which
are effectively an extension to the concept of the global variable discussed in Chapter 8,
“Working with Functions.”
An external variable is one whose value can be accessed and changed by another
module. Inside the module that wants to access the external variable, the variable is
declared in the normal fashion and the keyword externis placed before the declaration.
This signals to the system that a globally defined variable from another file is to be
accessed.
Suppose you want to define an intvariable called moveNumber, whose value you
want to access and possibly modify from within a function contained in another file. In
Chapter 8, you learned that if you wrote the statement
int moveNumber = 0;
at the beginning of your program,outsideof any function, then its value could be refer-
enced by any function within that program. In such a case,moveNumberwas defined as a
global variable.
Actually, this same definition of the variable moveNumberalso makes its value accessi-
ble by functions contained in other files. Specifically, the preceding statement defines the
Free download pdf