The enumerated data type is a user-defined type based on the standard integer type. An enumeration consists of a set of named integer constants. In other words, in an enumerated type, each integer value is assigned an identifier.
ENUMERATED
DATA TYPE
The
enumerated data type is a user-defined type based on the standard integer type.
An enumeration consists of a set of named integer constants. In other words, in
an enumerated type, each integer value is assigned an identifier. This
identifier (which is also known as an enumeration constant) can be used as a
symbolic name to make the program more readable.
To
define enumerated data types, we use the keyword enum, which is the
abbreviation for ENUMERATE. Enu- merations create new data types to contain
values that are not limited to the values that fundamental data types may take.
The syntax of creating an enumerated data type can be given as follows:
enum enumeration_name { identifier1,
identifier2,….., identifiern };
The
enum keyword is basically used to declare and initialize a sequence of integer
constants. Here, enumeration_name is optional. Consider the following example,
which creates a new type of variable called COLORS to store colour constants.
enum COLORS (RED, BLUE, BLACK,
GREEN, YELLOW, PURPLE, WHITE};
Note
that no fundamental data type is used in the declaration of COLORS. After this
statement, COLORS has become a new data type. Here, COLORS is the name given to
the set of constants. In case you do not assign any value to a constant, the
default value for the first one in the list-RED (in our case) has the value of 0.
The rest of the undefined constants have a value 1 more than its previous one.
That is, if you do not initialize the constants, then each one would have a
unique value. The first would be zero and the rest would count upwards. So, in
our example,
RED = 0, BLUE = 1, BLACK 2, GREEN =
3, YELLOW = 4, PURPLE = 5, WHITE = 6
If
you want to explicitly assign values to these integer constants then you should
specifically mention those values shown as follows:
enum COLORS {RED = 2, BLUE, BLACK =
5, GREEN = 7, YELLOW, PURPLE, WHITE = 15};
As
a result of this statement, now RED = 2,
BLUE = 3, BLACK = 5, GREEN = 7, YELLOW = 8, PURPLE = 9, WHITE = 15.
Look
at the code which illustrates the declaration and access of enumerated data
types.
#include <stdio.h>
int main()
{enum {RED=2, BLUE, BLACK=5,
GREEN=7, YELLOW, PURPLE, WHITE=15};
printf("\n_RED = %d",
RED);
printf("\n BLUE = %d",
BLUE);
printf("\n BLACK = %d",
BLACK);
printf("\n GREEN = %d",
GREEN);
printf("\n YELLOW = %d",
YELLOW);
printf("\n PURPLE = %d",
PURPLE);
printf("\n WHITE = %d",
WHITE);
return 0;
}
Output
RED = 2
BLUE = 3
BLACK = 5
GREEN = 7
YELLOW = 8
PURPLE = 9
WHITE = 15
Note
The
value of an enumerator constant is always of the type int. Therefore, the
storage associated with an enumeration variable is the storage required for a
single int value. The enumeration constant or a value of the enumerated type
can be used anywhere in the program where the C language permits an integer
expression.
The
following rules apply to the members of an enumeration list:
•
An enumeration list may contain duplicate constant values. Therefore, two
different identifiers may be assigned the same value, say 3.
•
The identifiers in the enumeration list must be different from other
identifiers in the same scope with the same visibility including ordinary
variable names and identifiers in other enumeration lists.
•
Enumeration names follow the normal scoping rules. So every enumeration name
must be different from other enumeration, structure, and union names with the
same visibility.
Note
If
we create an enumerated type without enumeration_name, it is known as an
anonymous enumerated type. For example, enum {OFF, ON}; declares an enumerated
type that has two constants OFF with a value 0 and ON with a value 1.
We
have seen that enumerated constants are basically integers, so programs with
statements such as int fore_color RED;
is considered to be a legal statement in C.
In
extension to this, C also permits the user to declare variables of an
enumerated data type in the same way as we create variables of other basic data
types. The syntax for declaring a variable of an enumerated data type can be
given as
enumeration_name variable_name;
So
to create a variable of COLORS, we may write
enum COLORS bg_color;
This
declares a variable called bg_color, which is of the enumerated data type,
COLORS. Another way to declare a variable can be as illustrated in the
following statement.
enum COLORS (RED, BLUE, BLACK,
GREEN, YELLOW, PURPLE, WHITE} bg_color, fore_color;
C
also permits to use the typedef keyword for enumerated data types. For example,
if we write
typedef enum COLORS color;
Then,
we can straightaway declare variables by writing
color forecolor = RED;
Once
the enumerated variable has been declared, values can be stored in it. However,
an an enumerated variable can hold only declared values for the type. For
example, to assign the colur black to the background colour, we will write,
bg_color = BLACK;
An
important thing to note here is that once an enumerated variable has been
assigned a value, we can store its value in another variable of the same type.
The following statements illustrate this concept.
enum
COLORS bg_color, border_color;
bg_color
= BLACK;
border_color
= bg_color;
Enumerated
types can be implicitly or explicitly cast. For example, the compiler can
implicitly cast an enumerated type to an integer when required. However, when
we implicitly cast an integer to an enumerated type, the compiler will either
generate an error or a warning message.
To
understand this, answer one question. If we write
enum COLORS (RED, BLUE, BLACK,
GREEN, YELLOW, PURPLE, WHITE};
enum COLORS C;
C = BLACK + WHITE;
Here,
c is an enumerated data type variable. If we write C = BLACK + WHITE, then logically, it should be 2 + 6 = 8, which is
basically a value of type int. However, the left-hand side of the assignment
operator is of the type enum COLORS. So the statement would report an error. To
remove the error, you can do either of two things. First, declare c to be an
int. Second, cast the right-hand side in the following manner
C = enum COLORS (BLACK + WHITE);
To
summarize,
enum COLORS (RED, BLUE, BLACK,
GREEN, YELLOW, PURPLE, WHITE};
enum COLORS C;
с = BLACK; //valid in C
c = 2; // illegal in C
c = (enum COLORS) 2; // Right way
C
also allows using comparison operators on enumerated data type. Look at the
following statements, which illustrate this concept.
bg_color = (enum COLORS) 6;
if (bg_color = WHITE)
fore_color = BLUE;
fore_color = BLACK;
if (bg_color == fore_color)
printf("\n NOT VISIBLE");
Since
enumerated types are derived from integer type, they can be used in a switch
case statement. The following code demonstrates the use of the enumerated type
in a switch case statement.
enum (RED, BLUE, BLACK, GREEN,
YELLOW, PURPLE, WHITE )bg_color;
switch (bg_color)
{
case RED:
case BLUE:
case GREEN:
printf("\n It is a primary
color");
break;
case default:
printf("\n It is not a primary
color");
break;
}
Since
enumerated types are derived types, they cannot be read or written using
formatted input/output functions available in the C language. When we read or
write an enumerated type, we read/write it as an integer. The compiler would
implicitly do the type conversion as discussed earlier. The following
statements illustrate this concept.
enum COLORS (RED, BLUE, BLACK,
GREEN, YELLOW, PURPLE, WHITE};
enum COLORS c;
scanf("%d", &c);
printf("\n Color = %d",
c);
16.
Write a program to display the name of the colours using an enumerated type.
#include <stdio.h>
enum COLORS (RED, BLUE, BLACK,
GREEN, YELLOW, PURPLE, WHITE);
int main()
{
enum COLORS C;
char *color_name [] =
{"RED", "BLUE",
"BLACK", "GREEN", "YELLOW", "PURPLE","WHITE"};
for (c = RED; c <= WHITE; C++)
printf("\n %s",
color_name [c] );
return 0;
}
OR
#include <stdlib.h>
#include <conio.h>
enum COLORS {red, blue, black,
green, yellow, purple, white};
int main()
{
enum COLORS c;
с = rand()%7;
switch(c)
{
case red: printf("\n
RED"); break;
case blue: printf("\n
BLUE"); break;
case black: printf("\n
BLACK"); break;
case green: printf("\n
GREEN"); break;
case yellow: printf("\n
YELLOW"); break;
case purple: printf("\n
PURPLE"); break;
case white: printf("\n
WHITE"); break;
}
return 0;
Output
GREEN
Programming in C: Unit IV: Structures and Union : Tag: : with Example C Programs - Enumerated Data Type
Programming in C
CS3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation