C type enum : unscoped enumeration


C 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;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 in C++;inherited by C++.The scoped enumeration on the other hand is a newly added features in C++11.We can say that 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.

To define an enumeration we will use the keyword enum,followed by a braces -opening and closing braces- and a semi-colon.So 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 };

enum value_enum { val1 , val2 , val3 };

Note while defining the second enumeration a name “value_enum” is given to it,but the first one has no name.Either of this 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 only 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 &lt< size &lt< endl
&lt< max_size ;

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


Uninitialized enumerators

Enumeration rule allows the programmer to use the enumerators without initialization.This means if we hadn’t assigned value to any 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.

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 .


 


Stop wasting time,earn money($$$) from your website-Join Now!

Some properties of enumeration and enumerators

Property One ::Enumeration scope

The scoping rule still applies in enumeration.If the enumeration has a local scope then the enumerators of that enum can only be accessed in the local area.And off course global scope enum can be accessed from anywhere within the file.

Link:variables scope:local variables and global variables

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

int main( )
{
cout<< size << ” ” << char_size ;

{
enum { loc=7 , loc_size=90 }; ///local scope enum

cout << array_size << endl //fine
<< loc_size ; //fine
}

cout << max_size ; ///fine

cout << loc ; //error ,local scope

return 0 ;
}

Property 2:Enumerators type is ‘const int’

All the types of enumerators in any enum type is “cont int” type.You cannot change the type of enumerators.And since the type is a “const int” type,we cannot change the value of the enumerators later again.Once it has been assigned a value the enumerator is fixated to that value for it’s life time.If the enumerators were not assigned any value ,in this case also we cannot change it’s value later.

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

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

int main( )
{
array_size=67 ; //error
size=78; //error

eval1=12 ; // error

return 0 ;
}

Since we cannot change the value of enumerator,it doesn’t mean we cannot use it to assign to another variable or enumerator.Consider the example below.

int i1=array_size ;//ok

evalue eval5=eval1 ; //ok
evalue eval6=evalue::eval2; ///ok

The second and third code declare new enumerators of type ‘evalue’.Note we are initializing newly created enumerators here,not trying to change it’s value,so the ‘const int’ type rule-unchangingness- is still followed.

Property 3:Enumerators cannot have same name.

Enumerators belong to different or same enum type cannot have the same name.Using the same name for different enumerators create conflict with the previously defined enumerators.

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

enum data_size { char_size , //error previously defined
int_size };

This is also one of the difference enum has with user-defined type,where different structure can have members with same name.



Limitations of C type enum

One of the limitations of C type enumeration is that the type of enumerators is fix to integer type.We cannot change it’s type whatsoever.However,this limitation is overcome by C++11 scoped enumeration which allow defining enumerators of different types.Not only that,C++11 enum has lot of other useful properties which is worth using to write a better program.