* better
[mascara-docs.git] / lang / C / the.ansi.c.programming.language / notes.accompany.ansi.c / sx4n.html
blob4c1b6fa9d8c05fdcff60bfc5a7582260c9c3a7c7
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. -->
5 <html>
6 <head>
7 <link rev="owner" href="mailto:scs@eskimo.com">
8 <link rev="made" href="mailto:scs@eskimo.com">
9 <title>section 1.10: External Variables and Scope</title>
10 <link href="sx4m.html" rev=precedes>
11 <link href="sx5.html" rel=precedes>
12 <link href="sx4.html" rev=subdocument>
13 </head>
14 <body>
15 <H2>section 1.10: External Variables and Scope</H2>
17 page 31
18 <p>There's a bit of jargon in this section.
19 An <dfn>external</dfn> variable is what is sometimes called a global
20 variable.
21 The authors introduce the term <dfn>automatic</dfn> to refer to the
22 local variables we've seen so far; this is a good word to
23 remember, even if you never use it, because people will spring
24 it on you when they're being precise, and if you don't know
25 this usage you'll think they're talking about transmissions or
26 something.
27 (To be precise,
28 ``local'' is a broader category than ``automatic'';
29 there are both automatic and <TT>static</TT> local variables.)
30 </p><p>Deep sentence:
31 <blockquote>If [automatic variables] are not set,
32 they will contain garbage.
33 </blockquote>Actually,
34 if automatic variables <em>always</em> contained garbage,
35 the situation wouldn't be quite so bad.
36 In practice,
37 they often
38 (though not always)
39 do contain zero or some other predictable value,
40 and this happens just often enough to lull you into the
41 occasional false sense of security,
42 by making
43 a program with an inadvertently uninitialized variable
44 seem
45 to work.
47 </p><p>Deep sentence:
48 <blockquote>An external variable must be <I>defined</I>,
49 exactly once,
50 outside of any function; this sets aside storage for it.
51 The variable must also be <I>declared</I> in each function that
52 wants to access it;
53 this states the type of the variable.
54 </blockquote>The basic rule is ``define once; declare many times.''
55 As we'll see just below,
56 it is not necessary for a declaration of an external variable
57 to appear in every single function;
58 it is possible for one external declaration to apply to many
59 functions.
60 (In the clause
61 ``the variable must also be declared in each function'',
62 the word ``declared'' is an adjective, not a verb.)
64 </p><p>page 33
65 </p><p>In fact, the ``common practice'' of placing
66 ``definitions of all external variables at the beginning of the source file''
67 is so common that it's rare to see external declarations within functions,
68 as in the functions on page 32.
69 The authors are using the in-function <TT>extern</TT>
70 declarations partly because it is an alternative style, and
71 partly because we haven't talked about separate compilation
72 (that is, building a single program from several separate
73 source files) yet.
74 Rather than jumping the gun and discussing those two topics now,
75 I'll just mention that the discussion in section 1.10
76 might be a bit misleading,
77 and that you should probably wait
78 until we get to the complete description of the issue in section 4.4
79 before you commit any of this to memory.
80 </p><p>Deep sentence:
81 <blockquote>You should note that we are using the words
82 <I>definition</I> and <I>declaration</I> carefully when we refer
83 to external variables in this section.
84 ``Definition'' refers to the place where the variable is created
85 or assigned storage;
86 ``declaration'' refers to places where the nature of the variable
87 is stated but no storage is allocated.
88 </blockquote>Do note the careful distinction;
89 it's an important one and one which I'll be using, too.
90 </p><p>page 34
91 </p><p>The authors' criticism of the second (page 32) version of the
92 longest-line program is accurate.
93 The revision of the longest-line program to use external variables
94 was done only to demonstrate the use of external variables,
95 not to improve the program in any way
96 (nor does it improve the program in any way).
97 </p><p>As a general rule,
98 external variables are acceptable for storing certain kinds of
99 global state information which never changes,
100 which is needed in many functions,
101 and which would be a nuisance to pass around.
102 I don't think of external variables as
103 ``communicating between functions''
104 but rather as
105 ``setting common state for the entire program.''
106 When you start thinking of an external variables as being one
107 of the ways you communicate with a particular function,
108 and in particular when you find yourself changing the value of
109 some external variable just before calling some function,
110 to affect its operation in some way,
111 you start getting into the troublesome uses of external variables,
112 which you should avoid.
113 </p><hr>
115 Read sequentially:
116 <a href="sx4m.html" rev=precedes>prev</a>
117 <a href="sx5.html" rel=precedes>next</a>
118 <a href="sx4.html" rev=subdocument>up</a>
119 <a href="top.html">top</a>
120 </p>
122 This page by <a href="http://www.eskimo.com/~scs/">Steve Summit</a>
123 // <a href="copyright.html">Copyright</a> 1995, 1996
124 // <a href="mailto:scs@eskimo.com">mail feedback</a>
125 </p>
126 </body>
127 </html>