I was messing around with C and discovered an apparent bug in GCC:
#include <stdio.h>
int y = (1, 2);
int main()
{
return 0;
}
in gcc gives error: initializer element is not constant
while it works fine in clang. And as expected, putting the declaration inside the main()
function also works with both compilers.
The parentheses are necessary, as otherwise gcc would be correct in disallowing the comma operator.
Despite the gcc code snippet looking like this:
3 | int y = (1, 2);
| ^
the parentheses are not the problem. int y = (1 + 2);
works fine.
I know that the comma operator is never necessary in constant expressions, as there should never be any side effects, but it's still odd that GCC sees it as an error instead of just printing warning: expression result unused
(which it also does).
Is this a bug?