* X more docs for C
[mascara-docs.git] / C / the.ansi.c.programming.language / notes.accompany.ansi.c / sx7f.html
blobf7d78c3a927198299af62b3f0eb09d766704ff1d
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 4.6: Static Variables</title>
10 <link href="sx7e.html" rev=precedes>
11 <link href="sx7g.html" rel=precedes>
12 <link href="sx7.html" rev=subdocument>
13 </head>
14 <body>
15 <H2>section 4.6: Static Variables</H2>
17 page 83
18 <p>Deep sentence:
19 <blockquote>The <TT>static</TT> declaration,
20 applied to an external variable or function,
21 limits the scope of that object
22 to the rest of the source file being compiled.
23 External <TT>static</TT> thus provides a way to hide names like
24 <TT>buf</TT> and <TT>bufp</TT> in the
25 <TT>getch</TT>-<TT>ungetch</TT> combination,
26 which must be external so they can be shared,
27 yet which should not be visible
28 to users of <TT>getch</TT> and <TT>ungetch</TT>.
29 </blockquote>So we can have three kinds of declarations:
30 local to one function,
31 restricted to one source file,
32 or global across potentially many source files.
33 We can imagine other possibilities,
34 but these three cover most needs.
35 </p><p>Notice that the <TT>static</TT> keyword does two completely different things.
36 Applied to a local variable
37 (one inside of a function),
38 it modifies the lifetime (``duration'') of the variable
39 so that it persists for as long as the program does,
40 and does not disappear between invocations of the function.
41 Applied to a variable outside of a function
42 (or to a function)
43 <TT>static</TT> limits the scope to the current file.
44 </p><p>To summarize the scope of external and <TT>static</TT> functions and variables:
45 when a function or global variable is defined without <TT>static</TT>,
46 its scope is <em>potentially</em> the entire program,
47 although any file which wishes to use it will generally need an
48 <TT>extern</TT> declaration.
49 A definition with <TT>static</TT> limits the scope
50 by <em>prohibiting</em> other files
51 from accessing a variable or function;
52 even if they try to use an <TT>extern</TT> declaration,
53 they'll get errors about ``undefined externals.''
54 </p><p>The rules for declaring and defining functions and global variables,
55 and using the <TT>extern</TT> and <TT>static</TT> keywords,
56 are admittedly complicated and somewhat confusing.
57 You don't need to memorize all of the rules right away:
58 just use simple declarations and definitions at first,
59 and as you find yourself needing some of the more complicated
60 possibilities such as <TT>static</TT> variables,
61 the rules will begin to make more sense.
62 </p><hr>
63 <p>
64 Read sequentially:
65 <a href="sx7e.html" rev=precedes>prev</a>
66 <a href="sx7g.html" rel=precedes>next</a>
67 <a href="sx7.html" rev=subdocument>up</a>
68 <a href="top.html">top</a>
69 </p>
70 <p>
71 This page by <a href="http://www.eskimo.com/~scs/">Steve Summit</a>
72 // <a href="copyright.html">Copyright</a> 1995, 1996
73 // <a href="mailto:scs@eskimo.com">mail feedback</a>
74 </p>
75 </body>
76 </html>