In the first instance, the compiler itself is trying to pick an integer that is causing an overflow, and so is warning you. It is likely producing INT_MIN
. The standard allows for any value in a signed int
to be an enum constant (see bottom).
In the second, the expression (INT_MAX + 1)
is calculated before it is assigned to out_2
. An overflow in the expression here is producing a result that is allowed, but this is undefined behaviour. The valid result is then stored in the enum, which is why the first error isn't produced.
clang (3.2) will also not warn about this, which is effectively identical:
int a = INT_MAX + 1;
In this respect, clang is not behaving according to the C standard, as this is undefined.
The output from gcc in comparison makes the difference completely clear:
In function ‘main’:
9:9: error: overflow in enumeration values
13:25: warning: integer overflow in expression [-Woverflow]
The Intel compiler ignores the enum overflow, but warns about the integer overflow:
enum.c(13): warning #61: integer operation result is out of range
out_2 = INT_MAX + 1
^
For reference, from the C99 standard 6.7.7.2.2, "The expression that de?nes the value of an enumeration constant shall be an integer constant expression that has a value representable as an
int
; .3, "The identi?ers in an enumerator list are declared as constants that have type
int
and may appear wherever such are permitted." i.e. an enum constant may be any
int
value, and has an
int
type. The resulting type of a defined enum variable can be
char
,
int
or
unsigned int
, as long as it allows for all the possible constants in the enum. As such both
enums
in the example are undefined, as they both require an integer overflow. The first is explicitly illegal.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…