Enumerations C

  • What are enumerations?
  • Why enumerations are used?
  • Use of enumerations.
  • Weakness of enumerations.

Enumerations are a way of defining constants. Here we will see what are enumerations? What is the need of enumerations? How we use them and the drawbacks of enumerations.

What are enumerations?
Enumerations are data-type like structures and unions.
In general enumerations may be defined as

enum tag {member1, member2,…, member n};

enum is the requird keyword, tag is the name of the enumeration and member1…membern are members (identifiers). Each member is a constant. The constant represent integer values. The member names must differ from each other.
It is similar to structures i.e once an enumeration has been defined, variable of that type may be defined.

enum tag var1, var2……varn:

The definition of enum and variable declaration may be combined together

enum tag {mem1, mem2, ….mem n} var1, var2;


enum colors {black, blue, cyan, green, magenta, red, white, yellow);

enum colors background, foreground;
Colors are the names of the enumeration.
Black, blue is the members of the enumeration which are called as enumeration constants.
Enumerations constants are automatically assigned integer values internally, starting with 0 for first member with each successive member increasing by 1.
i.e. member 1 is assigned 0
member 2 is assigned 1
. .
member n is assigned n -1
In our example
black 0
blue 1
cyan 2
green 3
magenta 4
red 5
white 6
yellow 7
If we want to have a different integer value assigned to the constants, it can be done by explicitly assigning value to the constant.

enum colors {black = 10, blue = 20, cyan = 30, green = 40, magenta = 50, red = 60, white = 70, yellow = 80};

If we don’t assign explicit values to few of them, they will automatically be assigned values which increase successively by 1 from the last explicit assignment.

enum colors {black = -1, blue, cyan, green, magenta, red = 2, white, yellow};

The enumeration constants will represent the following integer values.
black -1
cyan 0
green 2
magenta 3
red 2
white 3
yellow 4
Values need not be distinct in same enumeration. The enumeration variables are generally assigned enumeration constants rather than the integer values. They can be used like any other integer constants.

Why enumerations are used
Enumeration variables are particularly useful as flags to indicate various options or to identify various conditions; it increases the clarity of the program.
If I want to have color for the background, it is easier to understand with
background = blue;

Use of enumeration
Following example shows how enumerated data types are used. If we need to use all departments in a certain program, the departments are easier to understand if we use assembly, manufacturing, toolroom etc instead of integer values like 0, 1, 2…

# include<string.h>
void main()
{ enum emp_dept { assembly, manufacturing, accounts, stores};
struct employee {
char name [30];
int age;
float bs;
enum emp_dept dept; };
struct employee e;
strcpy (e.name, "Loather Mattheus");
e .age = 28;
e .bs = 5575.50;
e .dept = manufacturing;
printf (" Name = %s",e.name);
printf (" Age = %d", e.age);
printf (" Basic salary = %f",e.bs);
printf (" Department = %d", e.dept);
if (e.dept == accounts)
printf (" %s is an accountant", e.name);
printf (" %s is not an accountant", e.name);

Name = Loather Mattheus
Age = 28
Basic salary = 5575.50
Deparement = 1
Loather Mattheus is not an account

Weakness of Enumerations
When we use printf statement to print enumeration variables, the enumeration constant cannot be printed. Only the equivalent integer value can be printed. Ordinary integer variables can be used in place of enumeration variables. Thus enumeration variables do not provide any fundamentally new capabilities.

Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

C Topics