C Programming

Enumerated data type

The enumerated data type is used to define more than one integer symbolic constants. For example,

          enum boolean (false, true};

Here, enum is a keyword, boolean is data type and false and true are possible values. The value of false is 0 and true is 1. Once enumerated data type is defined, we can create variables as like other data types. For example,

          enum boolean status;

where status is variable of type Boolean and can possess only values false and true. The following assignment is valid.

          status = false;

Study the following examples of enumerated data types to get more information.

example 1:

enum month (JAN=1,FEB,MAR,APR, MAY,JUN,JUL, AUG, SEP, OCT,NOV, DEC) Here, JAN is assigned value 1, FEB = 2, MAR = 3 and so on. The symbols defined in enumerated data types can be compared also. For example, JAN is less than MAR. Consider the following example where the condition with if is true.

          enum month m1;
          enum month m2;
          m1 = JAN;
          m2 = MAR;
          if(ml < m2)
          {
                    ...
                    ...
          }

example 2: 

enum escap {BeLL='\a',NEWLINE='\n',VTAB='\v',TAB='\t'}

Here, different values are assigned to each symbol.

The symbolic constant defines only one symbol at a time. while enumerated data type can define many symbolic constant at a time. The main advantage of enumerated data type is that we can assign names and process easily.