When I convert enum to integer type, what can I expect?
Does the first variant have value of 0?
Does the variant ordinal number increment by 1?
Assume that I did not explicitly provide the values.
If you like code, then the questions are here:
enum foo
{
A,
B,
};
int
main(void)
{
assert(A == 0);
assert(B == 1);
}
CodePudding user response:
Does the first variant have value of 0?
Yes.
Does the variant ordinal number increment by 1?
Yes.
What guarantees does C provide about enum ordinal values
From https://port70.net/~nsz/c/c11/n1570.html#6.7.2.2 :
[...] If the first enumerator has no =, the value of its enumeration constant is 0. 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. [...]