* remove "\r" nonsense
[mascara-docs.git] / C / the.ansi.c.programming.language / notes.accompany.ansi.c / sx5d.html
blob27a6351a0735fb1b25fbf76c83e2fa3502e0c0e7
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 2.4: Declarations</title>
10 <link href="sx5c.html" rev=precedes>
11 <link href="sx5e.html" rel=precedes>
12 <link href="sx5.html" rev=subdocument>
13 </head>
14 <body>
15 <H2>section 2.4: Declarations</H2>
17 page 40
18 <p>You may wonder <em>why</em> variables must be declared before use.
19 There are two reasons:
20 </p><OL><li>It makes
21 things somewhat
22 easier on the compiler;
23 it knows right away what kind of storage to allocate
24 and what code to emit to store and manipulate
25 each variable;
26 it doesn't have to try to intuit the programmer's intentions.
27 <li>It forces a bit of useful discipline on the programmer:
28 you cannot introduce variables willy-nilly;
29 you must think about them enough to pick appropriate types for them.
30 (The compiler's error messages to you,
31 telling you that you apparently forgot to declare a variable,
32 are as often helpful as they are a nuisance:
33 they're helpful when they tell you that you misspelled a variable,
34 or forgot to think about exactly how you were going to use it.)
35 </OL><p>Although there are a few places where
36 ``certain declarations can be made implicitly by context'',
37 making use of these removes the advantages of reason
39 above,
40 so I recommend always declaring everything explicitly.
41 </p><p>Most of the time,
42 I recommend writing one declaration per line
43 (as in the ``latter form'' on page 40).
44 For the most part, the compiler doesn't care what order declarations are in.
45 You can order the declarations alphabetically,
46 or in the order that they're used,
47 or to put related declarations next to each other.
48 Collecting all variables of the same type together on one line
49 essentially orders declarations by type,
50 which isn't a very useful order
51 (it's only slightly more useful than random order).
52 </p><p>If you'd rather not remember the rules for default initialization
54 (namely that ``external or static variables are initialized to
55 zero by default'' and ``automatic variables for which there is no
56 initializer have... garbage values''),
57 you can get in the habit of initializing everything.
58 It never hurts to explicitly initialize something
59 when it would have been implicitly initialized anyway,
60 but forgetting to initialize something that needs it can be the
61 source of frustrating bugs.
62 </p><p>Don't worry about the distinction between ``external or static variables'';
63 we haven't seen it yet.
64 </p><p>One mild surprise is that <TT>const</TT> variables are <em>not</em>
65 ``constant expressions'' as defined on page 38.
66 You can't say something like
67 <pre> const int maxline = 1000;
68 char line[maxline+1]; /* WRONG */
69 </pre></p><hr>
70 <p>
71 Read sequentially:
72 <a href="sx5c.html" rev=precedes>prev</a>
73 <a href="sx5e.html" rel=precedes>next</a>
74 <a href="sx5.html" rev=subdocument>up</a>
75 <a href="top.html">top</a>
76 </p>
77 <p>
78 This page by <a href="http://www.eskimo.com/~scs/">Steve Summit</a>
79 // <a href="copyright.html">Copyright</a> 1995, 1996
80 // <a href="mailto:scs@eskimo.com">mail feedback</a>
81 </p>
82 </body>
83 </html>