20.09.2015 Views

Programming in C

Kochan - ProgramminginC

Kochan - ProgramminginC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

340 Chapter 15 Work<strong>in</strong>g with Larger Programs<br />

double x;<br />

static double result;<br />

static void doSquare (void)<br />

{<br />

double square (void);<br />

}<br />

x = 2.0;<br />

result = square ();<br />

<strong>in</strong>t ma<strong>in</strong> (void)<br />

{<br />

doSquare ();<br />

pr<strong>in</strong>tf ("%g\n", result);<br />

}<br />

return 0;<br />

extern double x;<br />

double square(void)<br />

{<br />

}<br />

return x * x;<br />

mod1.c<br />

mod2.c<br />

Figure 15.1<br />

Communication between modules.<br />

mod1.c def<strong>in</strong>es two global variables: x and result,both of type double. x can be<br />

accessed by any module that is l<strong>in</strong>ked together with mod1.c. On the other hand, the<br />

keyword static <strong>in</strong> front of the def<strong>in</strong>ition of result means that it can only be accessed<br />

by functions def<strong>in</strong>ed <strong>in</strong>side mod1.c (namely ma<strong>in</strong> and doSquare).<br />

When execution beg<strong>in</strong>s, the ma<strong>in</strong> rout<strong>in</strong>e calls doSquare.This function assigns the<br />

value 2.0 to the global variable x and then calls the function square. Because square is<br />

def<strong>in</strong>ed <strong>in</strong> another source file (<strong>in</strong>side mod2.c), and because it doesn’t return an <strong>in</strong>t,<br />

doSquare properly <strong>in</strong>cludes an appropriate declaration at the beg<strong>in</strong>n<strong>in</strong>g of the function.<br />

The square function returns as its value the square of the value of the global variable<br />

x. Because square wants to access the value of this variable, which is def<strong>in</strong>ed <strong>in</strong> another<br />

source file (<strong>in</strong> mod1.c), an appropriate extern declaration appears <strong>in</strong> mod2.c (and, <strong>in</strong> this<br />

case, it makes no difference whether the declaration occurs <strong>in</strong>side or outside the square<br />

function).<br />

The value that is returned by square is assigned to the global variable result <strong>in</strong>side<br />

doSquare, which then returns back to ma<strong>in</strong>.Inside ma<strong>in</strong>, the value of the global variable<br />

result is displayed.This example, when run, produces a result of 4.0 at the term<strong>in</strong>al<br />

(because that’s obviously the square of 2.0).<br />

Study the example until you feel comfortable with it.This small—albeit impractical—<br />

example illustrates very important concepts about communicat<strong>in</strong>g between modules, and<br />

it’s necessary that you understand these concepts to work effectively with larger<br />

programs.

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

Saved successfully!

Ooh no, something went wrong!