20.09.2015 Views

Programming in C

Kochan - ProgramminginC

Kochan - ProgramminginC

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Communication Between Modules<br />

339<br />

Static Versus Extern Variables and Functions<br />

You now know that any variable def<strong>in</strong>ed outside of a function is not only a global variable,<br />

but is also an external variable. Many situations arise <strong>in</strong> which you want to def<strong>in</strong>e a<br />

variable to be global but not external. In other words, you want to def<strong>in</strong>e a global variable<br />

to be local to a particular module (file). It makes sense to want to def<strong>in</strong>e a variable<br />

this way if no functions other than those conta<strong>in</strong>ed <strong>in</strong>side a particular file need access to<br />

the particular variable.This can be accomplished <strong>in</strong> C by def<strong>in</strong><strong>in</strong>g the variable to be<br />

static.<br />

The statement<br />

static <strong>in</strong>t moveNumber = 0;<br />

if made outside of any function, makes the value of moveNumber accessible from any subsequent<br />

po<strong>in</strong>t <strong>in</strong> the file <strong>in</strong> which the def<strong>in</strong>ition appears, but not from functions conta<strong>in</strong>ed <strong>in</strong><br />

other files.<br />

If you need to def<strong>in</strong>e a global variable whose value does not have to be accessed from<br />

another file, declare the variable to be static.This is a cleaner approach to programm<strong>in</strong>g:The<br />

static declaration more accurately reflects the variable’s usage and no conflicts<br />

can be created by two modules that unknow<strong>in</strong>gly both use different external global<br />

variables of the same name.<br />

As mentioned earlier <strong>in</strong> this chapter, you can directly call a function def<strong>in</strong>ed <strong>in</strong><br />

another file. Unlike variables, no special mechanisms are required; that is, to call a function<br />

conta<strong>in</strong>ed <strong>in</strong> another file, you don’t need an extern declaration for that function.<br />

When a function is def<strong>in</strong>ed, it can be declared to be extern or static, the former<br />

case be<strong>in</strong>g the default. A static function can be called only from with<strong>in</strong> the same file as<br />

the function appears. So, if you have a function called squareRoot, plac<strong>in</strong>g the keyword<br />

static before the function header declaration for this function makes it callable only<br />

from with<strong>in</strong> the file <strong>in</strong> which it is def<strong>in</strong>ed:<br />

static double squareRoot (double x)<br />

{<br />

...<br />

}<br />

The def<strong>in</strong>ition of the squareRoot function effectively becomes local to the file <strong>in</strong> which<br />

it is def<strong>in</strong>ed. It cannot be called from outside the file.<br />

The same motivations previously cited for us<strong>in</strong>g static variables also apply to the case<br />

of static functions.<br />

Figure 15.1 summarizes communication between different modules. Here two modules<br />

are depicted, mod1.c and mod2.c.<br />

mod1.c def<strong>in</strong>es two functions: doSquare and ma<strong>in</strong>.The way th<strong>in</strong>gs are set up here,<br />

ma<strong>in</strong> calls doSquare, which <strong>in</strong> turn calls square.This last function is def<strong>in</strong>ed <strong>in</strong> the<br />

module mod2.c.<br />

Because doSquare is declared static, it can only be called from with<strong>in</strong> mod1.c, and<br />

by no other module.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!