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. -->
7 <link rev=
"owner" href=
"mailto:scs@eskimo.com">
8 <link rev=
"made" href=
"mailto:scs@eskimo.com">
9 <title>section
4.9: Initialization
</title>
10 <link href=
"sx7h.html" rev=precedes
>
11 <link href=
"sx7j.html" rel=precedes
>
12 <link href=
"sx7.html" rev=subdocument
>
15 <H2>section
4.9: Initialization
</H2>
18 <p>These are
<em>some
</em> of the rules on initialization;
19 we'll learn a few more later
20 as we learn about a few more data types.
21 </p><p>If you don't feel like memorizing the rules for default initialization,
22 just go ahead and explicitly initialize everything you care about.
23 </p><p>Earlier we said that C is quite general in its treatment of expressions:
24 anywhere you can use an expression,
25 you can use any expression.
26 Here's an exception to that rule:
27 in an initialization of an external or static variable
28 (strictly speaking, any variable of static duration;
29 generally speaking, any global variable
30 or local
<TT>static
</TT> variable),
31 the initializer must be a constant expression,
32 with value determinable at compile time,
33 without calling any functions.
34 (This rule is easy to understand:
35 since these initializations
37 conceptually at compile time,
38 before the program starts running,
39 there's no way for a function call--that is,
40 some run-time action--to be involved.)
42 </p><p>It probably won't concern you right away,
43 but it turns out that there's another exception
44 about the allowable expressions in initializers:
45 in the brace-enclosed list of initializers for an array,
46 all of the expressions must be constant expressions
47 (even for local arrays).
49 </p><p>There is an error in some printings:
50 if there are fewer explicit initializers than required for an array,
51 the others will be initialized to zero,
52 for external, static,
<em>and
</em> automatic (local) arrays.
53 (When an automatic array has no initializers at all,
54 then it contains garbage,
55 just as simple automatic variables do.)
56 </p><p>If the initialization
57 <pre> char pattern[] =
"ould";
58 </pre>makes sense to you, you're fine.
59 But if the statement that
60 <pre> char pattern[] =
"ould";
61 </pre>is equivalent to
62 <pre> char pattern[] = { 'o', 'u', 'l', 'd', '\
0' };
63 </pre>bothers you at all,
64 study it until it makes sense.
65 Also, note that a character array which seems to contain
68 actually contains five, because of the terminating
<TT>'\
0'
</TT>.
72 <a href=
"sx7h.html" rev=precedes
>prev
</a>
73 <a href=
"sx7j.html" rel=precedes
>next
</a>
74 <a href=
"sx7.html" rev=subdocument
>up
</a>
75 <a href=
"top.html">top
</a>
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>