Enumerated Data Types: The Enumerated data type allows the programmer to invent his own data type and decide what value the variables of this data type may carry. Enum or enumerator is an integer or number with defined meaningful names. Enum is often used inside C programs to replace a hard coded numbers with some meaningful symbols.

Advantages of Enumerated Data Type:

  1. Makes program listing more readable.
  2. Using enumerators reduces programming error.

Let's see one example situation -

/* A function prints state of a task with numbers */
void print_task_state (int state)
{
  switch (state) {
    csase 0:
      printf ("Task has been added.\n");
      break;
    case 1:
      printf("Task is ready.\n");
      break;
    case 2:
      printf("Task is running.\n");
      break;
    case 3:
      printf("Task is waiting.\n");
      break;
    case 4:
      printf("Task has been terminated.\n");
      break;
	default:
      printf("Task in undefined state.\n");
  }
}

This example has task state in hard coded numbers. It is difficult to remember these numbers. Enum can be used here. Let's re-write this same program with one enum type which will takecare this task state.

/* A function prints state of a task with enum */
enum state_t {
  TASK_ADDED,
  TASK_READY,
  TASK_RUNNING,
  TASK_WAITING,
  TASK_TERMINATED
};

void print_task_state (enum state_t state)
{
  switch (state) {
    csase TASK_ADDED:
      printf ("Task has been added.\n");
      break;
    case TASK_READY:
      printf("Task is ready.\n");
      break;
    case TASK_RUNNING:
      printf("Task is running.\n");
      break;
    case TASK_TERMINATED:
      printf("Task has been terminated.\n");
      break;
	default:
      printf("Task in undefined state.\n");
  }
}

From the above example this is clear that enum makes this example more readable and more manageable. Syntax and more on enums goes in the next section.


Syntax:
enum <enum name> {
  <Value name> [Optional = <number value>],
  ...
};


If not given any default value enum starts from 0;
It takes +1 of previous value to define next value unless we define explicitly.

enum state_t {
  TASK_ADDED,
  TASK_READY,
  TASK_RUNNING,
  TASK_WAITING,
  TASK_TERMINATED
}

/* Same as */
enum state_t {
  TASK_ADDED = 0,
  TASK_READY,
  TASK_RUNNING,
  TASK_WAITING,
  TASK_TERMINATED
}

/* Same as */
enum state_t {
  TASK_ADDED = 0,
  TASK_READY = 1,
  TASK_RUNNING = 2,
  TASK_WAITING = 3,
  TASK_TERMINATED = 4,
}

Example2:
If a program has to be designed which stores the sport which a person plays, then a enumerated data type sports may be invented which stores the four possible values: cricket, football, hockey and tennis.

The format of the enum is similar to that of a structure

enum sports
{
  cricket, football, hockey, tennis
};
enum sports n1, n2;

This declaration has two parts:

  • First part: declares data type and specifies possible values
  • Second part: declares variables of this data type

Values given to the variable has to be among the values given in the original declaration.
n1 = cricket
n2 = football

The internal permissible values of the enumerated data types correspond to integers starting with zero. In the above example, cricket corresponds to 0, football as 1, hockey as 2 and tennis as 3.

The method of assigning numbers by the compiler may be overridden by the programmer as shown below.

enum sports
{
  cricket = 10, football = 20, hockey = 30, tennis = 40
};
Uses

The main use of the enumerated data type is to make the listings in a program easier to read. Values like cricket, football are easier to read in a program than integers like 0, 1.

The following program explains the benefits of enums:

int main (int argc, char *argv[])
{
  enum sports
  {
    cricket, football, hockey, tennis
  };
  struct player
  {
    char name[50];
    int age;
    enum sports sport_played;
  };
  struct sports_man s;

  strcpy(s.name,"Sachin");
  s.age = 34;
  s.sport_played = cricket;

  printf("\nName : %s",s.name);
  printf("\nAge : %d",s.age);
  printf("\nSports Played : %d",s.sport_played);

}
Output Given:
Name : Sachin
Age : 34
Sport Played : 0

Deconstructing the program reveals that a data type enum sports has been defined with four values cricket, football, hockey and tennis. A variable sport_played has been defined in a structure player with two other elements.

Value is assigned to the variables in the structure. s.sport_played is assigned value cricket which is much more informitive than 0.

Although the value of s.sport_played is assigned value cricket, it actually receives the value 0 and this is the value shown when the variable is used in the program.

Functions like printf() and scanf() cannot use the enumerated values directly. It can only use the integer values.

You have viewed 1 page out of 248. Your C learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

address of local and dynamic variable, malloc vs calloc, NULL pointer, enum type, macro, macro vs constant, enum vs macro, macro vs typedef,

# C Programming Language (Prentice Hall Software)
# Let Us C Paperback - 2006 by Yashavant Kanetkar
# Understanding and Using C Pointers Core techniques for memory management
# Data Structures Using C and C++ Paperback - 1998
# Data Structures In C Paperback - August 11, 2008 by Noel Kalicharan