C type enum : unscoped enumeration

In C or C++ by enumeration or just enum we mean grouping together a set of const integral values as a single type.Like the user-defined type every enumeration is a unique type having it’s own rules and members also known as enumerators.In C++(including C++11 and C++14) there are two types of enumeration:scoped enumeration and unscoped enumeration.

The unscoped enumeration is the C way of defining enumeration also inherited by C++.

The scoped enumeration on the other hand is a newly added features in C++11.We can say that the scoped enumeration is a more dynamic form of the C type enumeration.

In this post we shall see how to define an unscoped enumeration and also see how to implement them in our program.The scoped enumeration is discuss in another post under C++11.


Unscoped enumeration

To define an enumeration we will use the keyword ‘enum‘,followed by a braces({}) and a semi-colon.Defining an enumeration is same as defining a structure or class type.The only difference is that while defining an enumeration we can either give a name for the enum type or drop the name.An example below shows how to define an enumeration.

enum { size , array_size , max_size }; //Enumeration has noname

enum value_enum { val1 , val2 , val3 };  //Enumeration has the name 'value_enum'

Either of the above enumeration is valid.

Enumerators:By enumerators we mean the members of the enumeration.For instance in the enumeration given above size , array_size , max-size,val1, val2 and val3 are the enumerators.


Initializing enumerators

Before using an enumeration it is better that we initialize the members of the enumeration.We can do this by assigning a value when the enumerators are declared.Note the enumerators can only be an integer type and you cannot do anything to change the type,so you can assign an integer literal or a char literal or a floating point literal.But,you cannot assign a string literal else you will get an error.

enum { size=10 ,
array_size=20.3 ,
max_size=49.00 ,
char_size=8
};

enum value_enum { val1 =’B’ , //work fine
val2=45.383 , //work fine
val3=”Shoes” //error!
} ;

Assigning string to ‘val3’ will give you an error.


Accessing enumerators

To access the enumerators value we can simply use the name of the enumerators.If the enumeration is given a name we can either use the enumerator name or use the scope resolution operator(::) to bind the names of the enum and the enumerators.An example code is given below.

/***accessing the enumerators of the enum without name */
cout << size << endl
<< max_size ;

/*** accessing the enumerators of the value_enum enum */
cout << value_enum::val1 << endl //work fine
<< value_enum::val3 << endl //fine
<< val2 ; //fine

Uninitialized enumerators

Enumeration rule allows the programmer to use the enumerators without initialization.This means if we haven’t assigned any value to enumerators the compiler would assign the enumerators starting from 0 and the value is incremented as it moves forward.Consider the code below.

enum { n1 , n2 , n3 }; //0 , 1 and 2 is assigned

cout << n1 << endl
<< n2 << endl
<< n3 ;

The output is,
 
0
1
2

If there were another enumerators next to n3 ,it would be given the value 3.So,the value is incremented for each successor enumerators.

Partially assigned enumerator

We can also partially assign value to some of the enumerators and leave out some other without any initialization.In such case the enumerator which is not assign any value will have the value of the predecessor incremented by 1.This rule apply for all the enumerators in any position.

enum { e1=5 , e3 , e4 , e5=90 , e6 , e7 };

cout<< e1 << endl //prints 5
<< e3 << endl //prints 6
<< e6 << endl //prints 91
<< e7 ; //prints 92

enum { eval1 , eval2 , eval3=-99 , eval4 };

cout << eval1 << endl //prints 0
<< eval4 ; //prints -98

For ‘eval4’ the predecessor value is -99,so incrementing it by 1 will give -98,which is assigned to ‘eval4’.

Check the next post for properties on unscoped enumeration Properties of enumeration and enumerators



Leave a Reply

Your email address will not be published. Required fields are marked *