Enumeration object set to a value not equal to any of its respective enumeration constants

后端 未结 3 1143
醉话见心
醉话见心 2020-12-11 18:09

What value does an enumeration object have if it is set to a value not equal to any of its respective enumeration constants?

Consider the following code:

<         


        
3条回答
  •  醉话见心
    2020-12-11 19:04

    enum foobar{
        FOO = 1,
        BAR = 5
    };
    
    enum foobar baz = 5;
    

    the baz declaration is equivalent to:

    enum foobar baz = BAR;
    

    as BAR is an int of value 5.

    This declaration is also valid:

    enum foobar qux = 42;
    

    C says an enum type is an integer type sufficiently large to represent all its enum constants. If the enum type is not sufficiently large, the value is converted to the enum integer type as for every integer type as per the rules of integer conversion (c99, 6.3.1.3). This integer conversion can be implementation-defined (see 6.3.1.3p3).

提交回复
热议问题