source: public/doc/gnu-c/Initializers.html@ 02598c2

Last change on this file since 02598c2 was 02598c2, checked in by Mikhail Kirillov <w96k@…>, on Oct 6, 2022 at 12:36:29 PM

Add gnu-c

  • Property mode set to 100644
File size: 7.0 KB
Line 
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
6licensed to the FSF.)
7
8Permission is granted to copy, distribute and/or modify this document
9under the terms of the GNU Free Documentation License, Version 1.3 or
10any later version published by the Free Software Foundation; with the
11Invariant Sections being "GNU General Public License," with the
12Front-Cover Texts being "A GNU Manual," and with the Back-Cover
13Texts as in (a) below. A copy of the license is included in the
14section entitled "GNU Free Documentation License."
15
16(a) The FSF's Back-Cover Text is: "You have the freedom to copy and
17modify this GNU manual. Buying copies from the FSF supports it in
18developing 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<!--
37a.summary-letter {text-decoration: none}
38blockquote.indentedblock {margin-right: 0em}
39div.display {margin-left: 3.2em}
40div.example {margin-left: 3.2em}
41div.lisp {margin-left: 3.2em}
42kbd {font-style: oblique}
43pre.display {font-family: inherit}
44pre.format {font-family: inherit}
45pre.menu-comment {font-family: serif}
46pre.menu-preformatted {font-family: serif}
47span.nolinebreak {white-space: nowrap}
48span.roman {font-family: initial; font-weight: normal}
49span.sansserif {font-family: sans-serif; font-weight: normal}
50ul.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>
60Next: <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> &nbsp; [<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&rsquo;s declaration, unless it is <code>extern</code>, should also
67specify its initial value. For numeric and pointer-type variables,
68the initializer is an expression for the value. If necessary, it is
69converted to the variable&rsquo;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
72local union-type (see <a href="Unions.html">Unions</a>) variable this way, from an
73expression whose value has the same type. But you can&rsquo;t initialize an
74array this way (see <a href="Arrays.html">Arrays</a>), since arrays are not first-class
75objects in C (see <a href="Limitations-of-C-Arrays.html">Limitations of C Arrays</a>) and there is no array
76assignment.
77</p>
78<p>You can initialize arrays and structures componentwise,
79with a list of the elements or components. You can initialize
80a union with any one of its alternatives.
81</p>
82<ul>
83<li> A component-wise initializer for an array consists of element values
84surrounded by &lsquo;<samp>{<span class="roman">&hellip;</span>}</samp>&rsquo;. If the values in the initializer
85don&rsquo;t cover all the elements in the array, the remaining elements are
86initialized to zero.
87
88<p>You can omit the size of the array when you declare it, and let
89the 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
96surrounded by &lsquo;<samp>{<span class="roman">&hellip;</span>}</samp>&rsquo;. Write the field values in the same
97order as the fields are declared in the structure. If the values in
98the initializer don&rsquo;t cover all the fields in the structure, the
99remaining 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>
103in the union definition.
104</li></ul>
105
106<p>For an array of arrays, a structure containing arrays, an array of
107structures, etc., you can nest these constructs. For example,
108</p>
109<div class="example">
110<pre class="example">struct point { double x, y; };
111
112struct 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
117number of elements for the sub-value they initialize, so that
118no elements or fields need to be filled in with zeros.
119But don&rsquo;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.
122Recall that the string constant includes an implicit null character at
123the end (see <a href="String-Constants.html">String Constants</a>). Using a string constant as
124initializer means to use its contents as the initial values of the
125array elements. Here are examples:
126</p>
127<div class="example">
128<pre class="example">char text[6] = &quot;text!&quot;; /* <span class="roman">Includes the null.</span> */
129char text[5] = &quot;text!&quot;; /* <span class="roman">Excludes the null.</span> */
130char text[] = &quot;text!&quot;; /* <span class="roman">Gets length 6.</span> */
131char text[]
132 = { 't', 'e', 'x', 't', '!', 0 }; /* <span class="roman">same as above.</span> */
133char text[] = { &quot;text!&quot; }; /* <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
137structures 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
140an array of <code>wchar_t</code>.
141</p>
142<hr>
143<div class="header">
144<p>
145Next: <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> &nbsp; [<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>
Note: See TracBrowser for help on using the repository browser.