开发者

Is enum addition defined in ANSI C?

开发者 https://www.devze.com 2023-03-12 17:27 出处:网络
Is it a guarantee that state after this code will be LX_DONE开发者_如何学Go? enum lx_state { LX_START, LX_MIDDLE, LX_DONE };

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
0

精彩评论

暂无评论...
验证码 换一张
取 消