* X more docs for C
[mascara-docs.git] / C / the.ansi.c.programming.language / c.programming.notes / sx4d.html
blob92ebd002afdf202a97faacbf67fe73ee482fde47
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.4 Examples</title>
10 <link href="sx4c.html" rev=precedes>
11 <link href="sx5.html" rel=precedes>
12 <link href="sx4.html" rev=subdocument>
13 </head>
14 <body>
15 <H2>4.4 Examples</H2>
17 <p>Here is an example demonstrating
18 almost
20 everything we've seen so far:
21 <pre>
22 int globalvar = 1;
23 extern int anotherglobalvar;
24 static int privatevar;
26 f()
28 int localvar;
29 int localvar2 = 2;
30 static int persistentvar;
32 </pre>
33 </p><p>Here we have six variables, three declared outside and three
34 declared inside of the function <TT>f()</TT>.
35 </p><p><TT>globalvar</TT> is a global variable.
36 The declaration we see is its defining instance
37 (it happens also to include an initial value).
38 <TT>globalvar</TT> can be used anywhere in this source file,
39 and it could be used in other source files, too
40 (as long as corresponding external declarations are issued in
41 those other source files).
42 </p><p><TT>anotherglobalvar</TT> is a second global variable.
43 It is <em>not</em> defined here;
44 the defining instance for it
45 (and its initialization)
47 somewhere else.
48 </p><p><TT>privatevar</TT> is a ``private'' global variable.
49 It can be used anywhere within this source file,
50 but functions in other source files cannot access it,
51 even if they try to issue external declarations for it.
52 (If other source files try to declare
54 a global variable called
55 ``<TT>privatevar</TT>'', they'll get their own;
56 they won't be sharing this one.)
57 Since it has static duration and receives no explicit initialization,
58 <TT>privatevar</TT> will be initialized to 0.
59 </p><p><TT>localvar</TT> is a local variable
60 within the function <TT>f()</TT>.
61 It can be accessed only within the function <TT>f()</TT>.
62 (If any other part of the program declares a variable named
63 ``<TT>localvar</TT>'',
64 that variable will be distinct from the one we're looking at here.)
65 <TT>localvar</TT> is conceptually ``created'' each
66 time <TT>f()</TT> is called, and disappears
67 when <TT>f()</TT> returns.
68 Any value which was stored in <TT>localvar</TT> last time
69 <TT>f()</TT> was running will be lost and will not be
70 available next time <TT>f()</TT> is called.
71 Furthermore,
72 since it has no explicit initializer,
73 the value of <TT>localvar</TT> will in general be garbage
74 each time <TT>f()</TT> is called.
75 </p><p><TT>localvar2</TT> is also local,
76 and everything that we said about <TT>localvar</TT> applies to it,
77 except that since its declaration includes an explicit initializer,
78 it will be initialized to 2
79 each time <TT>f()</TT> is called.
80 </p><p>Finally, <TT>persistentvar</TT> is again local to <TT>f()</TT>,
81 but it <em>does</em> maintain its value between calls to <TT>f()</TT>.
82 It has static duration but no explicit initializer,
83 so its initial value will be 0.
84 </p><p>The defining instances and external declarations we've been
85 looking at so far have all been of simple variables.
86 There are also defining instances and external declarations of
87 functions, which we'll be looking at in the next chapter.
88 </p><p>(Also, don't worry about static variables for now
89 if they don't make sense to you;
90 they're a relatively sophisticated concept,
91 which you won't need to use at first.)
92 </p><p>The term <dfn>declaration</dfn> is a general one which
93 encompasses defining instances and external declarations;
94 defining instances and external declarations are two different
95 kinds of declarations.
96 Furthermore, either kind of declaration suffices to inform the
97 compiler of the name and type of a particular variable (or function).
98 If you have the defining instance of a global variable in a
99 source file, the rest of that source file can use that variable
100 without having to issue any external declarations.
101 It's only in source files where the defining instance hasn't
102 been seen that you need external declarations.
103 </p><p>You will sometimes hear a defining instance referred to simply
104 as a ``definition,''
106 you will sometimes hear an external declaration referred to simply
107 as a ``declaration.''
108 These usages are mildly ambiguous, in that you can't tell out
109 of context whether a ``declaration'' is a generic
110 declaration
111 (that might be a defining instance or an external declaration)
112 or whether it's an external declaration that specifically is
113 not a defining instance.
114 (Similarly, there are other constructions that can be called
115 ``definitions'' in C, namely the definitions of
116 preprocessor macros, structures, and typedefs, none of which
117 we've met.)
118 In these notes, we'll try to make things clear by using the
119 unambiguous terms <dfn>defining instance</dfn>
120 and <dfn>external declaration</dfn>.
121 Elsewhere,
123 you may have to look at the context to determine how
124 the terms ``definition'' and
125 ``declaration'' are being used.
126 </p><hr>
128 Read sequentially:
129 <a href="sx4c.html" rev=precedes>prev</a>
130 <a href="sx5.html" rel=precedes>next</a>
131 <a href="sx4.html" rev=subdocument>up</a>
132 <a href="top.html">top</a>
133 </p>
135 This page by <a href="http://www.eskimo.com/~scs/">Steve Summit</a>
136 // <a href="copyright.html">Copyright</a> 1995-1997
137 // <a href="mailto:scs@eskimo.com">mail feedback</a>
138 </p>
139 </body>
140 </html>