Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do C++ enums Start at 0?

If I have an enum that does not assign numbers to the enumerations, will it's ordinal value be 0? For example:

enum enumeration { ZERO,                    ONE,                    TWO,                    THREE,                    FOUR,                    FIVE,                    SIX,                    SEVEN,                    EIGHT,                    NINE }; 

I've been able to find a post citing that the C99 standard requires a 0 ordinal number. But I know C++ ignores several things in the C99 standard. And I've also been able to find a post witnessing the compiler using an ordinal value of 1, something I also seem recall seeing, though I can't say how long ago that was.

I would really like to see an answer that confirms this for C++, but I'd also like to know if an ordinal 0 holds even if I specify a value in the middle of an enum:

enum enumeration { ZERO,                    ONE,                    TWO,                    THREE = 13,                    FOUR,                    FIVE,                    SIX,                    SEVEN,                    EIGHT,                    NINE }; 
like image 824
Jonathan Mee Avatar asked Jan 15 '16 12:01

Jonathan Mee


People also ask

Does an enum start at 0 or 1?

Enum ValuesThe first member of an enum will be 0, and the value of each successive enum member is increased by 1. You can assign different values to enum member. A change in the default value of an enum member will automatically assign incremental values to the other members sequentially.

What number do enums start at?

Enum Values By default, the first item of an enum has the value 0. The second has the value 1, and so on.

What is enum default value in C?

In C programming, an enumeration type (also called enum) is a data type that consists of integral constants. To define enums, the enum keyword is used. enum flag {const1, const2, ..., constN}; By default, const1 is 0, const2 is 1 and so on.

Is enum 0 based?

Rule description. The default value of an uninitialized enumeration, just like other value types, is zero.


1 Answers

Per that standard [dcl.enum]

The enumeration type declared with an enum-key of only enum is an unscoped enumeration, and its enumerators are unscoped enumerators. The enum-keys enum class and enum struct are semantically equivalent; an enumeration type declared with one of these is a scoped enumeration, and its enumerators are scoped enumerators. The optional identifier shall not be omitted in the declaration of a scoped enumeration. The type-specifier-seq of an enum-base shall name an integral type; any cv-qualification is ignored. An opaqueenum-declaration declaring an unscoped enumeration shall not omit the enum-base. The identifiers in an enumerator-list are declared as constants, and can appear wherever constants are required. An enumeratordefinition with = gives the associated enumerator the value indicated by the constant-expression. If the first enumerator has no initializer, the value of the corresponding constant is zero. An enumerator-definition without an initializer gives the enumerator the value obtained by increasing the value of the previous enumerator by one.

Emphasis mine

So yes, if you do not specify a start value, it will default to 0.

I would really like to see an answer that confirms this for C++, but I'd also like to know if an ordinal 0 holds even if I specify a value in the middle of an enum:

This also works. It will start at 0 and increment up along the way. Then after the enum you assign the value to it will begin to increase by one from that value for subsequent enumerator.

like image 186
NathanOliver Avatar answered Sep 21 '22 04:09

NathanOliver