Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enum variable default value?

The question is simple:

#include <iostream>  enum SomeEnum {       EValue1 = 1,       EValue2 = 4 };  int main() {     SomeEnum enummy;     std::cout << (int)enummy; } 

What will be the output?

Note: This is not an interview, this is code inherited by me from previous developers. Streaming operator here is just for example, actual inherited code doesn't have it.

like image 496
Haspemulator Avatar asked Jul 27 '11 10:07

Haspemulator


People also ask

Do enum values 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 is default value of enum in C++?

By default, the value of the first enumerator is zero if it is implicitly defined. The value of each subsequent implicitly-defined enumerator is the value of the previous enumerator + 1. (Optional) The name of a variable of the enumeration type.

What is default data type of enum?

The default value of an enumeration type E is the value produced by expression (E)0 , even if zero doesn't have the corresponding enum member. You use an enumeration type to represent a choice from a set of mutually exclusive values or a combination of choices.

What is the value of an enum?

An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.


1 Answers

The program has Undefined Behavior. The value of enummy is indeterminate. Conceptually there is no difference between your code and the following code:

int main() {    int i;          //indeterminate value    std::cout << i; //undefined behavior }; 

If you had defined your variable at namespace scope, it would be value initialized to 0.

enum SomeEnum {       EValue1 = 1,       EValue2 = 4,   }; SomeEnum e; // e is 0 int i;      // i is 0  int main() {     cout << e << " " << i; //prints 0 0  } 

Don't be surprised that e can have values different from any of SomeEnum's enumerator values. Each enumeration type has an underlying integral type(such as int, short, or long) and the set of possible values of an object of that enumeration type is the set of values that the underlying integral type has. Enum is just a way to conveniently name some of the values and create a new type, but you don't restrict the values of your enumeration by the set of the enumerators' values.

Update: Some quotes backing me:

To zero-initialize an object of type T means:
— if T is a scalar type (3.9), the object is set to the value of 0 (zero) converted to T;

Note that enumerations are scalar types.

To value-initialize an object of type T means:
— if T is a class type blah blah
— if T is a non-union class type blah blah
— if T is an array type, then blah blah — otherwise, the object is zero-initialized

So, we get into otherwise part. And namespace-scope objects are value-initialized

like image 71
Armen Tsirunyan Avatar answered Sep 22 '22 08:09

Armen Tsirunyan