* remove "\r" nonsense
[mascara-docs.git] / C / the.ansi.c.programming.language / c.programming.notes / sx4b.html
blobd7748d8590ebce1e257c92066003fcf18059f6bf
1 <!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
2 <!-- This collection of hypertext pages is Copyright 1995-7 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>4.2 Visibility and Lifetime (Global Variables, etc.)</title>
10 <link href="sx4ba.html" rev=precedes>
11 <link href="sx4c.html" rel=precedes>
12 <link href="sx4.html" rev=subdocument>
13 </head>
14 <body>
15 <H2>4.2 Visibility and Lifetime (Global Variables, etc.)</H2>
17 <p>We haven't said so explicitly,
18 but variables are channels of communication within a program.
19 You set a variable to a value at one point in a program,
20 and at another point (or points) you read the value out again.
21 The two points may be in adjoining statements,
22 or they may be in widely separated parts of the program.
23 </p><p>How long does a variable last?
24 How widely separated can the setting and fetching parts of the program be,
25 and how long after a variable is set does it persist?
26 Depending on the variable
27 and how you're using it,
28 you might want different answers to
29 these questions.
30 </p><p>The <dfn>visibility</dfn> of a variable determines how much of
31 the rest of the program can access that variable.
32 You can arrange that a variable is visible
33 only within one part of one function,
34 or in one function,
35 or in one source file,
37 anywhere in the program.
38 (We haven't really talked about source files yet; we'll be
39 exploring them soon.)
40 </p><p>Why would you want to limit the visibility of a variable?
41 For maximum flexibility, wouldn't it be handy if all variables
42 were potentially visible everywhere?
43 As it happens, that arrangement would be <em>too</em> flexible:
44 everywhere in the program, you would have to keep track of the
45 names of all the variables declared anywhere else in the program,
46 so that you didn't accidentally re-use one.
47 Whenever a variable had the wrong value by mistake, you'd have
48 to search the entire program for the bug, because any statement
49 in the entire program could potentially have modified that variable.
50 You would constantly be stepping all over yourself by using a
51 common variable name like <TT>i</TT> in two parts of your program,
52 and having one snippet of code accidentally overwrite the
53 values being used by another part of the code.
54 The communication would be sort of like an old party
55 line--you'd always be accidentally interrupting other
56 conversations, or having your conversations interrupted.
57 </p><p>To avoid this confusion,
58 we generally give variables the narrowest or smallest
59 visibility they need.
60 A variable declared within the braces <TT>{}</TT> of a function
61 is visible only within that function;
62 variables declared within functions are called <dfn>local variables</dfn>.
63 If another function somewhere else declares a local variable
64 with the same name, it's a different variable entirely,
66 and the two don't
67 clash
69 with each other.
70 </p><p>On the other hand, a variable declared outside of any function
71 is a <dfn>global variable</dfn>, and it is potentially visible anywhere
72 within the program.
73 You use global variables when you <em>do</em> want the communications path
74 to be able to travel to any part of the program.
75 When you declare a global variable, you will usually give it a
76 longer, more descriptive name
77 (not something generic like <TT>i</TT>)
78 so that whenever you use it you will remember that it's the
79 same variable everywhere.
81 </p><p>Another word for the visibility of variables is <dfn>scope</dfn>.
82 </p><p>How long do variables last?
83 By default, local variables
84 (those declared within a function)
85 have <dfn>automatic duration</dfn>:
86 they spring into existence when the function is called,
87 and they (and their values) disappear when the function returns.
88 Global variables, on the other hand, have <dfn>static duration</dfn>:
89 they last, and the values stored in them persist,
90 for as long as the program does.
91 (Of course, the values can in general still be overwritten,
92 so they don't necessarily persist forever.)
94 </p><p>Finally, it is possible to split a function up into several
95 source files, for easier maintenance.
96 When several source files are combined into one program
97 (we'll be seeing how in the next chapter)
98 the compiler must have a way of correlating the global
99 variables which might be used to communicate between the
100 several source files.
102 Furthermore, if a global variable is going to be useful for communication,
103 there must be exactly one of it:
104 you wouldn't want one function in one source file
105 to store a value in one global variable named <TT>globalvar</TT>,
106 and then have another function in another source file
107 read from a <em>different</em> global variable named <TT>globalvar</TT>.
108 Therefore,
109 a global variable should
110 have exactly one <dfn>defining instance</dfn>,
111 in one place in one source file.
112 If the same variable is to be used anywhere else
113 (i.e. in some other source file or files),
114 the variable is declared
115 in those other file(s)
116 with an <dfn>external declaration</dfn>,
117 which is not a defining instance.
118 The external declaration says,
119 ``hey, compiler,
120 here's the name and type of a global variable I'm going to use,
121 but don't define it here,
122 don't allocate space for it;
123 it's one that's defined somewhere else,
124 and I'm just referring to it here.''
125 If you accidentally have two distinct defining instances for a
126 variable of the same name,
127 the compiler (or the linker) will complain that it is
128 ``multiply defined.''
129 </p><p>It is also possible to have a variable which is global in the
130 sense that it is declared outside of any function,
131 but private to the one source file it's defined in.
132 Such a variable is visible to the functions in that source file
134 but not to any functions in any other source files,
135 even if they try to issue a matching declaration.
136 </p><p>You get any extra control you might need over visibility and
137 lifetime, and you distinguish between defining instances and
138 external declarations, by
139 using
140 <dfn>storage classes</dfn>.
141 A storage class is an extra keyword
142 at the beginning of
143 a declaration
144 which modifies the declaration in some way.
145 Generally, the storage class
146 (if any)
147 is the first word in the declaration,
148 preceding the type name.
149 (Strictly speaking,
150 this ordering has not traditionally been necessary,
151 and you may see some code
152 with the storage class, type name,
153 and other parts of a declaration
154 in an unusual order.)
155 </p><p>We said that, by default, local variables had automatic duration.
156 To give them static duration
157 (so that, instead of coming and going as the function is
158 called, they persist for as long as the function does),
159 you precede their declaration with the <TT>static</TT> keyword:
160 <pre>
161 static int i;
162 </pre>
163 </p><p>By default, a declaration of a global variable
164 (especially if it specifies an initial value)
165 is the defining instance.
166 To make it an external declaration,
167 of a variable which is defined somewhere else,
169 precede it with the keyword <TT>extern</TT>:
170 <pre>
171 extern int j;
172 </pre>
174 </p><p>Finally, to arrange that a global variable is visible only
175 within its containing source file,
177 precede it with the <TT>static</TT> keyword:
178 <pre>
179 static int k;
180 </pre>
182 </p><p>Notice that the <TT>static</TT> keyword
183 can do
185 two different things:
186 it adjusts the duration of a local variable from automatic to
187 static, or it adjusts the visibility of a global variable from
188 truly global to private-to-the-file.
189 </p><p>To summarize, we've talked about two different attributes of a
190 variable: visibility and duration.
191 These are orthogonal,
192 as shown in this table:
194 <table border>
195 <tr><td><td colspan=2 align="center">duration:</tr>
196 <tr><td>visibility:<td align="center">automatic<td align="center">static</tr>
197 <tr><td>local<td>normal local variables<td><TT>static</TT> local variables</tr>
198 <tr><td>global<td>N/A<td>normal global variables</tr>
199 </table>
201 We can also distinguish between file-scope global
202 variables and truly global variables, based on the presence or
203 absence of the <TT>static</TT> keyword.
204 </p><p>We can also distinguish between external declarations and
205 defining instances of
206 global variables, based on the presence or absence of the
207 <TT>extern</TT> keyword.
208 </p><hr>
210 Read sequentially:
211 <a href="sx4ba.html" rev=precedes>prev</a>
212 <a href="sx4c.html" rel=precedes>next</a>
213 <a href="sx4.html" rev=subdocument>up</a>
214 <a href="top.html">top</a>
215 </p>
217 This page by <a href="http://www.eskimo.com/~scs/">Steve Summit</a>
218 // <a href="copyright.html">Copyright</a> 1995-1997
219 // <a href="mailto:scs@eskimo.com">mail feedback</a>
220 </p>
221 </body>
222 </html>