1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
---|
2 | <html>
|
---|
3 | <!-- Copyright (C) 2022 Richard Stallman and Free Software Foundation, Inc.
|
---|
4 |
|
---|
5 | (The work of Trevis Rothwell and Nelson Beebe has been assigned or
|
---|
6 | licensed to the FSF.)
|
---|
7 |
|
---|
8 | Permission is granted to copy, distribute and/or modify this document
|
---|
9 | under the terms of the GNU Free Documentation License, Version 1.3 or
|
---|
10 | any later version published by the Free Software Foundation; with the
|
---|
11 | Invariant Sections being "GNU General Public License," with the
|
---|
12 | Front-Cover Texts being "A GNU Manual," and with the Back-Cover
|
---|
13 | Texts as in (a) below. A copy of the license is included in the
|
---|
14 | section entitled "GNU Free Documentation License."
|
---|
15 |
|
---|
16 | (a) The FSF's Back-Cover Text is: "You have the freedom to copy and
|
---|
17 | modify this GNU manual. Buying copies from the FSF supports it in
|
---|
18 | developing GNU and promoting software freedom." -->
|
---|
19 | <!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
|
---|
20 | <head>
|
---|
21 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
---|
22 | <title>Initializers (GNU C Language Manual)</title>
|
---|
23 |
|
---|
24 | <meta name="description" content="Initializers (GNU C Language Manual)">
|
---|
25 | <meta name="keywords" content="Initializers (GNU C Language Manual)">
|
---|
26 | <meta name="resource-type" content="document">
|
---|
27 | <meta name="distribution" content="global">
|
---|
28 | <meta name="Generator" content="makeinfo">
|
---|
29 | <link href="index.html" rel="start" title="Top">
|
---|
30 | <link href="Symbol-Index.html" rel="index" title="Symbol Index">
|
---|
31 | <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
|
---|
32 | <link href="Variables.html" rel="up" title="Variables">
|
---|
33 | <link href="Designated-Inits.html" rel="next" title="Designated Inits">
|
---|
34 | <link href="Combining-Variable-Declarations.html" rel="prev" title="Combining Variable Declarations">
|
---|
35 | <style type="text/css">
|
---|
36 | <!--
|
---|
37 | a.summary-letter {text-decoration: none}
|
---|
38 | blockquote.indentedblock {margin-right: 0em}
|
---|
39 | div.display {margin-left: 3.2em}
|
---|
40 | div.example {margin-left: 3.2em}
|
---|
41 | div.lisp {margin-left: 3.2em}
|
---|
42 | kbd {font-style: oblique}
|
---|
43 | pre.display {font-family: inherit}
|
---|
44 | pre.format {font-family: inherit}
|
---|
45 | pre.menu-comment {font-family: serif}
|
---|
46 | pre.menu-preformatted {font-family: serif}
|
---|
47 | span.nolinebreak {white-space: nowrap}
|
---|
48 | span.roman {font-family: initial; font-weight: normal}
|
---|
49 | span.sansserif {font-family: sans-serif; font-weight: normal}
|
---|
50 | ul.no-bullet {list-style: none}
|
---|
51 | -->
|
---|
52 | </style>
|
---|
53 |
|
---|
54 |
|
---|
55 | </head>
|
---|
56 |
|
---|
57 | <body lang="en">
|
---|
58 | <span id="Initializers"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Next: <a href="Designated-Inits.html" accesskey="n" rel="next">Designated Inits</a>, Previous: <a href="Variable-Declarations.html" accesskey="p" rel="prev">Variable Declarations</a>, Up: <a href="Variables.html" accesskey="u" rel="up">Variables</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
61 | </div>
|
---|
62 | <hr>
|
---|
63 | <span id="Initializers-1"></span><h3 class="section">20.2 Initializers</h3>
|
---|
64 | <span id="index-initializers"></span>
|
---|
65 |
|
---|
66 | <p>A variable’s declaration, unless it is <code>extern</code>, should also
|
---|
67 | specify its initial value. For numeric and pointer-type variables,
|
---|
68 | the initializer is an expression for the value. If necessary, it is
|
---|
69 | converted to the variable’s type, just as in an assignment.
|
---|
70 | </p>
|
---|
71 | <p>You can also initialize a local structure-type (see <a href="Structures.html">Structures</a>) or
|
---|
72 | local union-type (see <a href="Unions.html">Unions</a>) variable this way, from an
|
---|
73 | expression whose value has the same type. But you can’t initialize an
|
---|
74 | array this way (see <a href="Arrays.html">Arrays</a>), since arrays are not first-class
|
---|
75 | objects in C (see <a href="Limitations-of-C-Arrays.html">Limitations of C Arrays</a>) and there is no array
|
---|
76 | assignment.
|
---|
77 | </p>
|
---|
78 | <p>You can initialize arrays and structures componentwise,
|
---|
79 | with a list of the elements or components. You can initialize
|
---|
80 | a union with any one of its alternatives.
|
---|
81 | </p>
|
---|
82 | <ul>
|
---|
83 | <li> A component-wise initializer for an array consists of element values
|
---|
84 | surrounded by ‘<samp>{<span class="roman">…</span>}</samp>’. If the values in the initializer
|
---|
85 | don’t cover all the elements in the array, the remaining elements are
|
---|
86 | initialized to zero.
|
---|
87 |
|
---|
88 | <p>You can omit the size of the array when you declare it, and let
|
---|
89 | the initializer specify the size:
|
---|
90 | </p>
|
---|
91 | <div class="example">
|
---|
92 | <pre class="example">int array[] = { 3, 9, 12 };
|
---|
93 | </pre></div>
|
---|
94 |
|
---|
95 | </li><li> A component-wise initializer for a structure consists of field values
|
---|
96 | surrounded by ‘<samp>{<span class="roman">…</span>}</samp>’. Write the field values in the same
|
---|
97 | order as the fields are declared in the structure. If the values in
|
---|
98 | the initializer don’t cover all the fields in the structure, the
|
---|
99 | remaining fields are initialized to zero.
|
---|
100 |
|
---|
101 | </li><li> The initializer for a union-type variable has the form <code>{
|
---|
102 | <var>value</var> }</code>, where <var>value</var> initializes the <em>first alternative</em>
|
---|
103 | in the union definition.
|
---|
104 | </li></ul>
|
---|
105 |
|
---|
106 | <p>For an array of arrays, a structure containing arrays, an array of
|
---|
107 | structures, etc., you can nest these constructs. For example,
|
---|
108 | </p>
|
---|
109 | <div class="example">
|
---|
110 | <pre class="example">struct point { double x, y; };
|
---|
111 |
|
---|
112 | struct point series[]
|
---|
113 | = { {0, 0}, {1.5, 2.8}, {99, 100.0004} };
|
---|
114 | </pre></div>
|
---|
115 |
|
---|
116 | <p>You can omit a pair of inner braces if they contain the right
|
---|
117 | number of elements for the sub-value they initialize, so that
|
---|
118 | no elements or fields need to be filled in with zeros.
|
---|
119 | But don’t do that very much, as it gets confusing.
|
---|
120 | </p>
|
---|
121 | <p>An array of <code>char</code> can be initialized using a string constant.
|
---|
122 | Recall that the string constant includes an implicit null character at
|
---|
123 | the end (see <a href="String-Constants.html">String Constants</a>). Using a string constant as
|
---|
124 | initializer means to use its contents as the initial values of the
|
---|
125 | array elements. Here are examples:
|
---|
126 | </p>
|
---|
127 | <div class="example">
|
---|
128 | <pre class="example">char text[6] = "text!"; /* <span class="roman">Includes the null.</span> */
|
---|
129 | char text[5] = "text!"; /* <span class="roman">Excludes the null.</span> */
|
---|
130 | char text[] = "text!"; /* <span class="roman">Gets length 6.</span> */
|
---|
131 | char text[]
|
---|
132 | = { 't', 'e', 'x', 't', '!', 0 }; /* <span class="roman">same as above.</span> */
|
---|
133 | char text[] = { "text!" }; /* <span class="roman">Braces are optional.</span> */
|
---|
134 | </pre></div>
|
---|
135 |
|
---|
136 | <p>and this kind of initializer can be nested inside braces to initialize
|
---|
137 | structures or arrays that contain a <code>char</code>-array.
|
---|
138 | </p>
|
---|
139 | <p>In like manner, you can use a wide string constant to initialize
|
---|
140 | an array of <code>wchar_t</code>.
|
---|
141 | </p>
|
---|
142 | <hr>
|
---|
143 | <div class="header">
|
---|
144 | <p>
|
---|
145 | Next: <a href="Designated-Inits.html" accesskey="n" rel="next">Designated Inits</a>, Previous: <a href="Variable-Declarations.html" accesskey="p" rel="prev">Variable Declarations</a>, Up: <a href="Variables.html" accesskey="u" rel="up">Variables</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
146 | </div>
|
---|
147 |
|
---|
148 |
|
---|
149 |
|
---|
150 | </body>
|
---|
151 | </html>
|
---|