Wheninitializing an object ofscalar type, 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 C23):
=expression | (1) | ||||||||
={expression} | (2) | ||||||||
={} | (3) | (since C23) | |||||||
Because of the rules that apply to conversions as if by assignment,const andvolatile qualifiers on the declared type are ignored when determining which type to convert theexpression to.
Seeinitialization for the rules that apply when no initializer is used.
As with all other initializations,expression must be aconstant expression when initializing objects of static or thread-localstorage duration.
Theexpression cannot be acomma operator (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 automaticstorage duration are done as-if at execution time and are affected by thecurrent rounding; floating-point errors are reported as specified inmath_errhandling. 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 ONstaticfloat v=1.1e75;// does not raise exceptions: static init float u[]={1.1e75};// raises FE_INEXACTfloat 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) longdouble z=1.1e75;// does not raise exceptions (conversion is exact)}
#include <stdbool.h>int main(void){ bool b=true;constdouble d=3.14;int k=3.15;// conversion from double to intint 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}