1 <!DOCTYPE HTML PUBLIC
"-//W3O//DTD W3 HTML 2.0//EN">
2 <!-- This collection of hypertext pages is Copyright 1995, 1996 by Steve Summit. -->
3 <!-- This material may be freely redistributed and used -->
4 <!-- but may not be republished or sold without permission. -->
7 <link rev=
"owner" href=
"mailto:scs@eskimo.com">
8 <link rev=
"made" href=
"mailto:scs@eskimo.com">
9 <title>section
4.4: Scope Rules
</title>
10 <link href=
"sx7c.html" rev=precedes
>
11 <link href=
"sx7e.html" rel=precedes
>
12 <link href=
"sx7.html" rev=subdocument
>
15 <H2>section
4.4: Scope Rules
</H2>
18 <p>With respect to the ``practical matter''
19 of splitting the calculator program up into multiple source files,
20 though it's certainly small enough
21 to fit comfortably into a single source file,
22 it's not so small that there's anything wrong
23 with splitting it up into multiple source files,
24 especially if we start adding functionality to it.
25 </p><p>The
<dfn>scope
</dfn> of a name is what we have been calling its ``visibility.''
26 When we say things like ``calling a function with a prototype in scope''
27 we mean that a prototype is visible, that a declaration is in effect.
28 </p><p>The variables
<TT>sp
</TT> and
<TT>val
</TT> can be used by the
29 <TT>push
</TT> and
<TT>pop
</TT> routines because they're
30 defined in the same file
31 (and the definitions appear before
<TT>push
</TT> and
<TT>pop
</TT>).
32 They can't be used in
<TT>main
</TT> because no declaration for
33 them appears in
<TT>main.c
</TT>
34 (nor in
<TT>calc.h
</TT>, which
<TT>main.c
</TT> <TT>#include
</TT>s).
35 If
<TT>main
</TT> attempted to refer to
<TT>sp
</TT> or
<TT>val
</TT>,
36 they'd be flagged as undefined.
37 (Don't worry about the visibility of
38 ``
<TT>push
</TT> and
<TT>pop
</TT> themselves.'')
39 </p><p>The paragraph beginning ``On the other hand''
40 is explaining how global (``external'') variables like
<TT>sp
</TT>
41 and
<TT>val
</TT> could be accessed in a file other than the file
42 where they are defined.
43 In the examples we've been looking at,
45 <TT>sp
</TT> and
<TT>val
</TT> can be used in
<TT>push
</TT> and
<TT>pop
</TT>
46 because the variables are
47 defined above the functions.
48 If the variables were defined elsewhere
49 (i.e. in some other file),
51 declaration above--and
52 that's exactly what
<TT>extern
</TT> is for.
53 (See page
81 for an example.)
55 </p><p>A definition
<em>creates
</em> a variable,
57 for any given global variable,
58 you only want to do that once.
62 you want to refer to an existing variable,
64 without creating a new, conflicting one.
65 Referring to an existing variable or function
69 </p><p>Note also that the definition may optionally initialize the variable.
70 (Don't worry about why a declaration may optionally include an
72 </p><p>``This same organization would also be needed
73 if the definitions of
<TT>sp
</TT> and
<TT>val
</TT>
74 followed their use in one file''
75 means that we could conceivably have,
81 void push(double f) { ... }
82 double pop(void) { ... }
87 </pre>So ``
<TT>extern
</TT>'' just means ``somewhere else'';
88 it doesn't
<em>have
</em> to mean ``in a different file,''
89 though usually it does.
93 <a href=
"sx7c.html" rev=precedes
>prev
</a>
94 <a href=
"sx7e.html" rel=precedes
>next
</a>
95 <a href=
"sx7.html" rev=subdocument
>up
</a>
96 <a href=
"top.html">top
</a>
99 This page by
<a href=
"http://www.eskimo.com/~scs/">Steve Summit
</a>
100 //
<a href=
"copyright.html">Copyright
</a> 1995,
1996
101 //
<a href=
"mailto:scs@eskimo.com">mail feedback
</a>