* better
[mascara-docs.git] / lang / C / the.ansi.c.programming.language / c.programming.notes.int / sx4ea.html
blobcf9d613563f49a890d4e414ff79a43056cc2ba67
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>18.1.5: Storage Classes</title>
10 <link href="sx4da.html" rev=precedes>
11 <link href="sx4fa.html" rel=precedes>
12 <link href="sx4a.html" rev=subdocument>
13 </head>
14 <body>
15 <H3>18.1.5: Storage Classes</H3>
17 <p>A full-blown declaration in C consists of several parts:
18 the <dfn>storage class</dfn>,
19 the <dfn>base type</dfn>,
20 any <dfn>type qualifiers</dfn>,
21 and a list of <dfn>declarators</dfn>,
22 where each declarator consists of:
23 an <dfn>identifier</dfn>,
24 additional characters
25 possibly indicating that it is a pointer, array, or function,
26 and finally an optional <dfn>initializer</dfn>.
27 We've met each of these parts at various points along the way,
28 although we have never explicitly mentioned or defined the
29 <dfn>storage class</dfn>.
30 The storage class is optional;
31 it generally appears at the beginning of the declaration
32 (before the base type)
33 if it appears at all.
34 At most one storage class may appear in any one declaration.
35 </p><p>We've seen two storage classes so far, <TT>extern</TT> and
36 <TT>static</TT>.
37 <TT>extern</TT> marks a declaration as an
38 <dfn>external declaration</dfn>,
39 indicating that the identifier declared has its
40 <dfn>defining instance</dfn> somewhere else
41 (where ``somewhere else'' might be somewhere else in
42 the same source file,
43 or in a different source file).
44 <TT>static</TT> is used in two different ways,
45 (1) to indicate that a global (``file scope'')
46 variable is private to one source file, and cannot be accessed
47 (even with external declarations) from other source files,
49 (2) to indicate that a local variable should have static duration,
50 such that it does not come and go as the function is called and
51 returns,
52 and so that its value persists between invocations of the
53 function.
54 </p><p>Besides these two,
55 there are three other storage classes.
56 <TT>register</TT> indicates that the programmer believes that the
57 variable will be heavily used,
58 and that it should be assigned to a high-speed CPU register
59 (rather than an ordinary memory location) if possible.
60 Explicit <TT>register</TT> declarations are rare these days,
61 because modern compilers generally do an excellent job,
62 all by themselves and without any hints,
63 of deciding which variables belong in machine registers.
64 A limitation of <TT>register</TT> variables is that you cannot
65 generate pointers to them using the <TT>&amp;</TT> operator.
66 (This is because, on most machines,
67 pointers are implemented as memory addresses,
68 and CPU registers usually do not have memory addresses.)
69 </p><p>The fourth storage class is <TT>auto</TT>,
70 which indicates that a local variable should have automatic duration.
71 (Automatic duration, remember,
72 means that variables are automatically allocated when a function
73 is called and automatically deallocated when it returns.)
74 Since automatic duration is the default for local variables anyway,
75 <TT>auto</TT> is virtually never used;
76 it's a relic from C's past.
77 </p><p>The fifth storage class,
78 <TT>typedef</TT>, is described in the next section.
79 </p><hr>
80 <p>
81 Read sequentially:
82 <a href="sx4da.html" rev=precedes>prev</a>
83 <a href="sx4fa.html" rel=precedes>next</a>
84 <a href="sx4a.html" rev=subdocument>up</a>
85 <a href="top.html">top</a>
86 </p>
87 <p>
88 This page by <a href="http://www.eskimo.com/~scs/">Steve Summit</a>
89 // <a href="copyright.html">Copyright</a> 1996-1999
90 // <a href="mailto:scs@eskimo.com">mail feedback</a>
91 </p>
92 </body>
93 </html>