The enumeration object is set to a value not equal to any of its corresponding enumeration constants

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

Consider the following code:

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

enum foobar baz = 5;
enum foobar qux = 42;

The variable is bazset to an integer value 5, and the variable is quxset to an integer value 42.

I suspect the variable bazwill contain a value BAR, but I'm not sure about the variable qux. No enumeration constant has been assigned a value 42, so what happens when a variable is enum foobarset to that value?

Is the C99 standard explicit for the result?

+4
3

C99, -, , , . 6.7.2.2 :

char, . 110) .

, , , , . undefined 5, :

( , ), undefined.

, , , .

+1

qux enum. 42 , foobar, . , 42, , , .

, , enum, "flag", , :

enum foobar {
    foo = 1
,   bar = 2
,   baz = 4
} test = (foo | baz);

test 5, - enum.

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

enum foobar baz = 5;

baz :

enum foobar baz = BAR;

as BAR int 5.

:

enum foobar qux = 42;

C , enum , . enum , (c99, 6.3.1.3). (. 6.3.1.3p3).

+1

Source: https://habr.com/ru/post/1547963/


All Articles