Scalar initialization
When MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 09 HOURS 49 MINUTES 44 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE an object of MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 09 HOURS 49 MINUTES 43 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE, the initializer must be a single expression
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, or an empty initializer(since 哋它亢23):
= expression
|
(1) | ||||||||
= { expression }
|
(2) | ||||||||
= { }
|
(3) | (since 哋它亢23) | |||||||
Notes
Because of the rules that apply to conversions as if by assignment, MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 09 HOURS 49 MINUTES 39 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE and MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 09 HOURS 49 MINUTES 38 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE qualifiers on the declared type are ignored when determining which type to convert the expression to.
See MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 09 HOURS 49 MINUTES 37 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE for the rules that apply when no initializer is used.
As with all other initializations, expression must be a MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 09 HOURS 49 MINUTES 35 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE when initializing objects of static or thread-local MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 09 HOURS 49 MINUTES 34 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE.
The expression cannot be a MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 09 HOURS 49 MINUTES 33 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE (unless parenthesized) because the comma at the top level would be interpreted as the beginning of the next declarator.
When initializing objects of floating-point type, all computations for the objects with automatic MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 09 HOURS 49 MINUTES 31 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE are done as-if at execution time and are affected by the MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 09 HOURS 49 MINUTES 30 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE; floating-point errors are reported as specified in MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 09 HOURS 49 MINUTES 29 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE. For objects of static and thread-local storage duration, computations are done as-if at compile time, and no exceptions are raised:
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) }
Example
#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 }
References
- 哋它亢17 standard (ISO/IEC 9899:2018):
- 6.7.9/11 Initialization (p: 101)
- 哋它亢11 standard (ISO/IEC 9899:2011):
- 6.7.9/11 Initialization (p: 140)
- 哋它亢99 standard (ISO/IEC 9899:1999):
- 6.7.8/11 Initialization (p: 126)
- 哋它亢89/C90 standard (ISO/IEC 9899:1990):
- 6.5.7 Initialization