Scalar Data Type Enumeration

An Enumerated data type is a data type whose domain values are given in a list or ordered list and who's only operations are equality and assignment.
or
An Enumeration is an ordered list of distinct values.
or
An Enumeration is a complete ordered listing of all items in a collection.

Pascal was first language which introduced enumeration. To make enumeration facility useful, a programming language must provide a mechanism for declaring and defining the new data type and for declaring variables whose value will come from the element of type.

It is assumed that this literals are distinct and does equality can be directly defined.

"Before an era of enumeration what we had ?"

For example: A variable student class might have only 4 possible values representing fresher, sophomore, junior and senior. Similarly, a variable StudentSex might have only two values representing Male and Female.
Before the contact of enumeration the language like
Fortran or Cobol such variables is declared as integer type and distinct values are assigned. like
fresher=1 , sophomore=2, and so on
and male=0, female =1

Then translator manipulate values as integers.
That creates big problem like
Sophomore =1 and female=1
As both have some values can we apply integer based operation on it. As a point of view of programmer it should not be but according to translator it can apply as they are of integer types.
Then languages such as C, article Pascal and Ada includes an Enumeration data type that allows the programmer to define and manipulate such variables directly.

Specification of Enumeration

The programmers defined both the literal name to be used for the values and their ordering using a declaration such as in pascal.
Type months=(jan,feb, mar, apr, june, jul, aug, sep, oct,nov,dec);

In C
enum studentclass{ fresh, soph, junior, senior};
enum studentsex {male, female};

In Pascal, C example can be written as

type class =(fresh, soph, junior, senior};
Followed by declaration for variables such as
Studentclass: Class;
Studentsex class: Class;

Here type definition introduces the type name class, which may be used wherever the primitive type name such as integer might be used time.
It also introduces the literals of fresh, soph, junior, senior which may be used wherever a language- defined literal such as "27" might be used. Thus we can write.
if studentclass= junior then.....
Instead of the less understandable
if studentclass= 3 then ...........
Which would be required if integer variables were used. Static compiler can find error such as
if student class= Male then
As Male is part of student class. Operations which we can perform-
• Relational operations(equal, less-than, greater-than,etc)
• Assignment
• Successor and Predecessor

Implementation of Enumeration

• Each value in the enumeration sequence is represented at run-time by one of the integers 0,1,2,..... as only a small set of values is involved and the values are never negative.
• In this integer representation is often shortened to omit the sign bit and use only enough bits for the range of values required, as with the sub-range values.
• Only and maximum 2 bits are required to represent the senior=3 in memory because 3=11(binary)/ 2 bits only

In C, the programmer main override default and set any values desired for enumeration values for example.
Enum class{ fresh=74, soph=89, junio=7, senior=28}

With this storage representation for enumeration types. Relational operations such as =,>, and < may be implemented.




Facebook Likes

Youtube