With GCC 11.4 (-std=c99
) on Linux WSL, there is an compile error as in example 2) in the code below. I have not gotten this kind of error with other compilers such as TASKING, MULTI, ti-arm-clang.
Do you have any idea why this happens? Is calculation with float executed in runtime?
int array1[(int)(1.0)]={0};//no error
int array2[(int)(1.0+1.0)]={0};//error: variable-sized object may not be initialized
int array3[(int)(1.0)+(int)(1.0)]={0};//no error
int array5[(int)(1.f)]={0};//no error
With GCC 11.4 (-std=c99
) on Linux WSL, there is an compile error as in example 2) in the code below. I have not gotten this kind of error with other compilers such as TASKING, MULTI, ti-arm-clang.
Do you have any idea why this happens? Is calculation with float executed in runtime?
int array1[(int)(1.0)]={0};//no error
int array2[(int)(1.0+1.0)]={0};//error: variable-sized object may not be initialized
int array3[(int)(1.0)+(int)(1.0)]={0};//no error
int array5[(int)(1.f)]={0};//no error
- 2 This feels like an XY question, since there obvious ways to fix the failing code, some of them even presented in this question itself. – John Bollinger Commented Mar 23 at 13:41
1 Answer
Reset to default 7The C standard does not require implementations to perform floating-point arithmetic in constant expressions.
Likely, you are declaring these arrays outside of any function, so they have static storage duration. The size for a static array must be an integer constant expression. C 2024 6.6 specifies:
An integer constant expression shall have integer type and shall only have operands that are integer constants, named and compound literal constants of integer type, character constants,
sizeof
expressions whose results are integer constants,alignof
expressions, and floating, named, or compound literal constants of arithmetic type that are the immediate operands of casts. Cast operators in an integer constant expression shall only convert arithmetic types to integer types, except as part of an operand to the typeof operators,sizeof
operator, oralignof
operator.
Observe:
int array1[(int)(1.0)]={0};//no error
(1.0)
is a floating-point constant that is an immediate operand of a cast, so that is allowed. (Technically, 1.0
is inside parentheses, but we regard this as a grammatical grouping, not an operation.)
int array2[(int)(1.0+1.0)]={0};//error: variable-sized object may not be initialized
1.0+1.0
has floating-point constants that are not the immediate operands of casts. The C standard does not require that +
on floating-point operands be supported in constant expressions.
int array3[(int)(1.0)+(int)(1.0)]={0};//no error
Here (1.0)
is the immediate operand of a cast. Then the operands to +
are two int
values, which is supported in a constant expression.
int array5[(int)(1.f)]={0};//no error
Here (1.f)
is the immediate operand of a cast.
The C standard allows, but does not require, C implementations to accept other forms of constant expression. So some compilers may support floating-point arithmetic in constant expressions while others do not.