Is enum addition defined in ANSI C?
Is it a guarantee that state
after this code will be LX_DONE
开发者_如何学Go?
enum lx_state { LX_START, LX_MIDDLE, LX_DONE };
enum lx_state state = LX_START;
++state;
++state;
Yes, the C standard says, in 6.7.2.2/3,
Each subsequent enumerator with no = defines its enumeration constant as the value of the constant expression obtained by adding 1 to the value of the previous enumeration constant
enum
is an integer, so yes, state
will be LX_DONE
, assuming you get rid of the weird double ++
.
No, but it is guaranteed that the code won't compile.
enum lx_state { LX_START, LX_MIDDLE, LX_DONE };
int main() {
enum lx_state state = LX_START;
++(++state);
}
gives:
e.c: In function 'main':
e.c:6:2: error: lvalue required as increment operand
精彩评论