diff options
Diffstat (limited to 'devdocs/c/language%2Fscalar_initialization.html')
| -rw-r--r-- | devdocs/c/language%2Fscalar_initialization.html | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/devdocs/c/language%2Fscalar_initialization.html b/devdocs/c/language%2Fscalar_initialization.html new file mode 100644 index 00000000..d0fd3411 --- /dev/null +++ b/devdocs/c/language%2Fscalar_initialization.html @@ -0,0 +1,50 @@ + <h1 id="firstHeading" class="firstHeading">Scalar initialization</h1> <p>When <a href="initialization" title="c/language/initialization">initializing</a> an object of <a href="type#Type_groups" title="c/language/type">scalar type</a>, the initializer must be a single expression</p> +<p>The initializer for a scalar (an object of integer type including booleans and enumerated types, floating type including complex and imaginary, and pointer type including pointer to function) must be a single expression, optionally enclosed in braces<span class="t-rev-inl t-since-c23"><span>, or an empty initializer</span><span><span class="t-mark-rev t-since-c23">(since C23)</span></span></span>:</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">expression</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> <div class="t-li1"> +<span class="t-li">1,2)</span> The expression is evaluated, and its value, after <a href="conversion" title="c/language/conversion">conversion as if by assignment</a> to the type of the object, becomes the initial value of the object being initialized.</div> <div class="t-li1"> +<span class="t-li">3)</span> The object is <a href="initialization#Empty_initialization" title="c/language/initialization">empty-initialized</a>, i.e. initialized to numeric zero for an object of an arithmetic or enumeration type, or null pointer value for an object of a pointer type.</div> <h3 id="Notes"> Notes</h3> <p>Because of the rules that apply to conversions as if by assignment, <a href="const" title="c/language/const"><code>const</code></a> and <a href="volatile" title="c/language/volatile"><code>volatile</code></a> qualifiers on the declared type are ignored when determining which type to convert the <span class="t-spar">expression</span> to.</p> +<p>See <a href="initialization" title="c/language/initialization">initialization</a> for the rules that apply when no initializer is used.</p> +<p>As with all other initializations, <span class="t-spar">expression</span> must be a <a href="constant_expression" title="c/language/constant expression">constant expression</a> when initializing objects of static or thread-local <a href="storage_duration" title="c/language/storage duration">storage duration</a>.</p> +<p>The <span class="t-spar">expression</span> cannot be a <a href="operator_other#Comma_operator" title="c/language/operator other">comma operator</a> (unless parenthesized) because the comma at the top level would be interpreted as the beginning of the next declarator.</p> +<p>When initializing objects of floating-point type, all computations for the objects with automatic <a href="storage_duration" title="c/language/storage duration">storage duration</a> are done as-if at execution time and are affected by the <a href="../numeric/fenv/fe_round" title="c/numeric/fenv/FE round">current rounding</a>; floating-point errors are reported as specified in <a href="../numeric/math/math_errhandling" title="c/numeric/math/math errhandling"><code>math_errhandling</code></a>. For objects of static and thread-local storage duration, computations are done as-if at compile time, and no exceptions are raised:</p> +<div class="c source-c"><pre data-language="c">void f(void) +{ +#pragma STDC FENV_ACCESS ON + static float v = 1.1e75; // does not raise exceptions: static init + + float u[] = { 1.1e75 }; // raises FE_INEXACT + float w = 1.1e75; // raises FE_INEXACT + + double x = 1.1e75; // may raise FE_INEXACT (depends on FLT_EVAL_METHOD) + float y = 1.1e75f; // may raise FE_INEXACT (depends on FLT_EVAL_METHOD) + + long double z = 1.1e75; // does not raise exceptions (conversion is exact) +}</pre></div> <h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include <stdbool.h> +int main(void) +{ + bool b = true; + const double d = 3.14; + int k = 3.15; // conversion from double to int + int n = {12}, // optional braces + *p = &n, // non-constant expression OK for automatic variable + (*fp)(void) = main; + enum {RED, BLUE} e = RED; // enumerations are scalar types as well +}</pre></div> </div> <h3 id="References"> References</h3> <ul> +<li> C17 standard (ISO/IEC 9899:2018): </li> +<ul><li> 6.7.9/11 Initialization (p: 101) </li></ul> +<li> C11 standard (ISO/IEC 9899:2011): </li> +<ul><li> 6.7.9/11 Initialization (p: 140) </li></ul> +<li> C99 standard (ISO/IEC 9899:1999): </li> +<ul><li> 6.7.8/11 Initialization (p: 126) </li></ul> +<li> C89/C90 standard (ISO/IEC 9899:1990): </li> +<ul><li> 6.5.7 Initialization </li></ul> +</ul> <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/scalar_initialization" class="_attribution-link">https://en.cppreference.com/w/c/language/scalar_initialization</a> + </p> +</div> |
