diff options
| author | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
| commit | 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch) | |
| tree | f1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/c/language%2Finitialization.html | |
new repository
Diffstat (limited to 'devdocs/c/language%2Finitialization.html')
| -rw-r--r-- | devdocs/c/language%2Finitialization.html | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/devdocs/c/language%2Finitialization.html b/devdocs/c/language%2Finitialization.html new file mode 100644 index 00000000..7cacdb5b --- /dev/null +++ b/devdocs/c/language%2Finitialization.html @@ -0,0 +1,89 @@ + <h1 id="firstHeading" class="firstHeading">Initialization</h1> <p>A <a href="declarations" title="c/language/declarations">declaration</a> of an object may provide its initial value through the process known as <i>initialization</i>.</p> +<p>For each <a href="declarations" title="c/language/declarations">declarator</a>, the initializer, if not omitted, may be one of the following:</p> +<table class="t-sdsc-begin"> <tr class="t-sdsc"> <td> <code>=</code> <span class="t-spar">expression</span> </td> <td> (1) </td> <td class="t-sdsc-nopad"> </td> +</tr> <tr class="t-sdsc"> <td> <code>=</code> <code>{</code> <span class="t-spar">initializer-list</span> <code>}</code> </td> <td> (2) </td> <td class="t-sdsc-nopad"> </td> +</tr> <tr class="t-sdsc"> <td> <code>=</code> <code>{</code> <code>}</code> </td> <td> (3) </td> <td> <span class="t-mark-rev t-since-c23">(since C23)</span> </td> +</tr> +</table> <p>where <span class="t-spar">initializer-list</span> is a non-empty comma-separated list of <span class="t-spar">initializer</span> s (with an optional trailing comma), where each initializer has one of three possible forms:</p> +<table class="t-sdsc-begin"> <tr class="t-sdsc"> <td> <span class="t-spar">expression</span> </td> <td> (1) </td> <td class="t-sdsc-nopad"> </td> +</tr> <tr class="t-sdsc"> <td> <code>{</code> <span class="t-spar">initializer-list</span> <code>}</code> </td> <td> (2) </td> <td class="t-sdsc-nopad"> </td> +</tr> <tr class="t-sdsc"> <td> <code>{</code> <code>}</code> </td> <td> (3) </td> <td> <span class="t-mark-rev t-since-c23">(since C23)</span> </td> +</tr> <tr class="t-sdsc"> <td> <span class="t-spar">designator-list</span> <code>=</code> <span class="t-spar">initializer</span> </td> <td> (4) </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> +</tr> +</table> <table class="t-rev-begin"> <tr class="t-rev t-since-c99"> +<td> <p>where <span class="t-spar">designator-list</span> is a list of either array designators of the form <code>[</code> <span class="t-spar">constant-expression</span> <code>]</code> or struct/union member designators of the form <code>.</code> <span class="t-spar">identifier</span> ; see <a href="array_initialization" title="c/language/array initialization">array initialization</a> and <a href="struct_initialization" title="c/language/struct initialization">struct initialization</a>.</p> +<p>Note: besides initializers, brace-enclosed <span class="t-spar">initializer-list</span> may appear in <a href="compound_literal" title="c/language/compound literal">compound literals</a>, which are expressions of the form:</p> +<table class="t-sdsc-begin"> <tr class="t-sdsc"> <td> <code>(</code> <span class="t-spar">type</span> <code>)</code> <code>{</code> <span class="t-spar">initializer-list</span> <code>}</code> </td> <td class="t-sdsc-nopad"> </td> <td> </td> +</tr> <tr class="t-sdsc"> <td> <code>(</code> <span class="t-spar">type</span> <code>)</code> <code>{</code> <code>}</code> </td> <td class="t-sdsc-nopad"> </td> <td> <span class="t-mark-rev t-since-c23">(since C23)</span> </td> +</tr> +</table> </td> <td><span class="t-mark-rev t-since-c99">(since C99)</span></td> +</tr> </table> <h3 id="Explanation"> Explanation</h3> <p>The initializer specifies the initial value stored in an object.</p> +<h4 id="Explicit_initialization"> Explicit initialization</h4> <p>If an initializer is provided, see</p> +<ul> +<li> <a href="scalar_initialization" title="c/language/scalar initialization">scalar initialization</a> for the initialization of scalar types </li> +<li> <a href="array_initialization" title="c/language/array initialization">array initialization</a> for the initialization of array types </li> +<li> <a href="struct_initialization" title="c/language/struct initialization">struct initialization</a> for the initialization of struct and union types. </li> +</ul> <h4 id="Implicit_initialization"> Implicit initialization</h4> <p>If an initializer is not provided:</p> +<ul> +<li> objects with automatic <a href="storage_duration" title="c/language/storage duration">storage duration</a> are initialized to indeterminate values (which may be <a href="object" title="c/language/object">trap representations</a>) </li> +<li> objects with static and thread-local <a href="storage_duration" title="c/language/storage duration">storage duration</a> are empty-initialized </li> +</ul> <h4 id="Empty_initialization"> Empty initialization</h4> <table class="t-rev-begin"> <tr class="t-rev t-since-c23"> +<td> <p>An object is empty-initialized if it is explicitly initialized from initializer <code>= {}</code>.</p> +</td> <td><span class="t-mark-rev t-since-c23">(since C23)</span></td> +</tr> </table> <p>In some cases, an object is empty-initialized if it is not initialized explicitly, that is:</p> +<ul> +<li> pointers are initialized to null pointer values of their types </li> +<li> objects of integral types are initialized to unsigned zero </li> +<li> objects of floating types are initialized to positive zero </li> +<li> all elements of arrays, all members of structs, and the first members of unions are empty-initialized, recursively, plus all padding bits are initialized to zero </li> +</ul> (on platforms where null pointer values and floating zeroes have all-bit-zero representations, this form of initialization for statics is normally implemented by allocating them in the .bss section of the program image) <h3 id="Notes"> Notes</h3> <p>When initializing an object of static or thread-local <a href="storage_duration" title="c/language/storage duration">storage duration</a>, every <span class="t-spar">expression</span> in the initializer must be a <a href="constant_expression" title="c/language/constant expression">constant expression</a> or <a href="string_literal" title="c/language/string literal">string literal</a>.</p> +<p>Initializers cannot be used in declarations of objects of incomplete type, VLAs, and block-scope objects with linkage.</p> +<p>The initial values of function parameters are established as if by assignment from the arguments of a <a href="operator_other#Function_call" title="c/language/operator other">function call</a>, rather than by initialization.</p> +<p>If an indeterminate value is used as an argument to any standard library call, the behavior is undefined. Otherwise, the result of any expression involving indeterminate values is an indeterminate value (e.g. <code>int n;</code>, <code>n</code> may not compare equal to itself and it may appear to change its value on subsequent reads)</p> +<table class="t-rev-begin"> <tr class="t-rev t-until-c23"> +<td> <p>There is no special construct in C corresponding to <a href="https://en.cppreference.com/w/cpp/language/value_initialization" title="cpp/language/value initialization">value initialization</a> in C++; however, <code>= {0}</code><span class="t-rev-inl t-since-c99"><span> (or <code>(T){0}</code> in compound literals)</span><span><span class="t-mark-rev t-since-c99">(since C99)</span></span></span> can be used instead, as the C standard does not allow empty structs, empty unions, or arrays of zero length.</p> +</td> <td><span class="t-mark-rev t-until-c23">(until C23)</span></td> +</tr> <tr class="t-rev t-since-c23"> +<td> <p>The empty initializer <code>= {}</code> (or <code>(T){}</code> in compound literals) can be used to achieve the same semantics as <a href="https://en.cppreference.com/w/cpp/language/value_initialization" title="cpp/language/value initialization">value initialization</a> in C++.</p> +</td> <td><span class="t-mark-rev t-since-c23">(since C23)</span></td> +</tr> </table> <h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include <stdlib.h> +int a[2]; // initializes a to {0, 0} +int main(void) +{ + int i; // initializes i to an indeterminate value + static int j; // initializes j to 0 + int k = 1; // initializes k to 1 + + // initializes int x[3] to 1,3,5 + // initializes int* p to &x[0] + int x[] = { 1, 3, 5 }, *p = x; + + // initializes w (an array of two structs) to + // { { {1,0,0}, 0}, { {2,0,0}, 0} } + struct {int a[3], b;} w[] = {[0].a = {1}, [1].a[0] = 2}; + + // function call expression can be used for a local variable + char* ptr = malloc(10); + free(ptr); + +// Error: objects with static storage duration require constant initializers +// static char* ptr = malloc(10); + +// Error: VLA cannot be initialized +// int vla[n] = {0}; +}</pre></div> </div> <h3 id="References"> References</h3> <ul> +<li> C17 standard (ISO/IEC 9899:2018): </li> +<ul><li> 6.7.9 Initialization (p: 100-105) </li></ul> +<li> C11 standard (ISO/IEC 9899:2011): </li> +<ul><li> 6.7.9 Initialization (p: 139-144) </li></ul> +<li> C99 standard (ISO/IEC 9899:1999): </li> +<ul><li> 6.7.8 Initialization (p: 125-130) </li></ul> +<li> C89/C90 standard (ISO/IEC 9899:1990): </li> +<ul><li> 6.5.7 Initialization </li></ul> +</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/language/initialization" title="cpp/language/initialization">C++ documentation</a></span> for <span class=""><span>Initialization</span></span> </td> +</tr> </table> <div class="_attribution"> + <p class="_attribution-p"> + © cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br> + <a href="https://en.cppreference.com/w/c/language/initialization" class="_attribution-link">https://en.cppreference.com/w/c/language/initialization</a> + </p> +</div> |
