Enumerations


In C language, enumerations provide another way to create user-defined types. An enumeration type is designed for the objects that can have a limited set of values. For example, consider an application in which we want a variable to hold a Boolean value. We can create an enumeration type BOOL that have two values FALSE and TRUE. The values FALSE and TRUE are known as enumeration constants. The variables of type BOOLcan either have value FALSE or TRUE (i.e. Boolean value). Note that the values FALSEand TRUEare not the strings, but are integer constants. The compiler internally associates an integer value each with the names FALSE and TRUE. Thus, any operation that is applicable on an integer constant can be applied on them and any operation that is applicable on a variable of integer type can be applied on a variable of type BOOL. Since the integer values are represented by the names, the enumeration type helps in making the programs more readable. The important points about enumerations are as follows:

  1. Definition of an enumeration type: The general form of an enumeration-type definition is:
    [storage_class_specifier][type_qualifier] enum [tag-name] {enumeration-list}[identifier=initializer[,...]];
    
           

    The important points about the definition of an enumeration type are as follows:
    1. The terms enclosed within the square brackets are optional and might not be present in the definition of an enumeration type. The terms shown in bold are mandatory parts of an enumeration definition.
    2. An enumeration definition consists of a keyword enum, followed by an optional identifier name known as enumeration tag-name and a comma-separated list of enumerators enclosed within braces. All the enumerators present in the enumeration list forms an enumeration set.
    3. An enumerator is an identifier that can hold an integer value. It is also known as the enumeration constant. An integer value can optionally be assigned to an enumerator, e.g. in the enumeration-type definition enum BOOLEAN {true=1, false=0};, the integer constants 1 and 0 are assigned to the enumerators true and false, respectively.
    4. The names of the enumerators in the enumeration list must be unique.
    5. The values assigned to enumerators in the enumeration list need not be unique, e.g. the enumeration definition enum COLORS {red=2, green=1, yellow=1}; is perfectly valid.
    6. Each enumeration constant has a scope that begins just after its appearance in the enumeration list. Due to this rule, the enumeration definition enum COLORS {red=2, green=red, yellow=green}; is perfectly valid.
    7. Each enumerator in an enumeration list names a value. The enumeration constants are like symbolic constants except that their values are set automatically. By default, the first enumerator has the value 0. Each subsequent enumerator, if not explicitly assigned a value, has a value 1 greater than the value of the enumerator that immediately precedes it. The piece of code in Program 37 illustrates the interpretation of this rule.
      Program 37. A program to illustrate that the values of enumeration constants are set automatically
    8. The enumeration definition can optionally have the storage class specifier and type qualifiers. However, they should be used in an enumeration-type definition statement only if the objects of the defined enumeration type are declared at the same time.
    9. The enumeration definition is a statement and must be terminated with a semicolon.
  2. Declaring objects of an enumeration type: There are two ways to declare variables of an enumeration type:
    1. At the time of enumeration-type definition: Objects of an enumeration type can be declared at the time of enumeration-type definition. The variable declarations of the defined enumerated type given in Table 7 are valid.
      Table 9.7. Declaration of enumeration variables at the time of the enumeration-type definition
      enum BOOL {false, true} flag1, flag2; (a)enum {false, true} flag1, flag2; (b)

      The declarations of the constants of the defined enumerated type given in Table 8 are valid.
      Table 8. Declaration of the enumeration constants at the time of enumeration-type definition
      enum BOOL {false, true} const flag1=true, flag2=false; (a)const enum BOOL {false, true} flag1=true, flag2=false; (b)
      enum {false, true} const flag1=true, flag2=false; (c)const enum {false, true} flag1=true, flag2=false; (d)
    2. After enumeration-type definition in a separate declaration statement: Objects of an enumeration type can be created after its definition only if it is named or tagged. The keyword enum is used to declare the variables of the defined enumeration type. It is used in conjunction with the const qualifier to create the constants of the newly created type. The general form of declaring the objects of the defined enumeration type is:
      [storage_class_specifier][type_qualifier]enum named_eumeration_type identifier_name[=initializer[,...]];
      
           
      The important points about the declaration of an object of the defined enumeration type are as follows:
      1. The terms enclosed within the square brackets are optional and might not be present in an enumeration object declaration. The terms shown in bold are the mandatory parts of the enumeration object declaration.
      2. An enumeration object declaration consists of:
        1. The keyword enum for declaring the enumeration variables. The keyword enum in conjunction with theconst qualifier for declaring the constant of the defined enumeration type.
        2. The tag name of the defined enumeration type.
        3. Comma-separated list of identifiers (i.e. variable names and constant names). A variable can optionally be initialized by providing an initializer. However, the initialization of a constant is must.
        4. An object of an enumeration type can be initialized with another object of the same enumeration type or with one of the enumerators present in the enumeration list or with an integer value. The piece of code in Program 38 illustrates this fact.
      Program 38. A program that illustrates the initialization of an enumeration object
  3. Operations on the objects of an enumeration type: The following operations can be performed on the object of an enumeration type:
    1. Size of an enumeration object or enumeration type: An enumeration object holds an enumerator, which in fact is an integer constant. Thus, when the sizeof operator is applied on an enumeration object or an enumeration type, it outputs the size of an integer. The piece of code in Program 39 illustrates this fact.
      Program 39. A program that illustrates the use of the sizeof operator on an enumeration type and an enumeration object
    2. Address-of an enumeration object: The address-of operator can be applied on an enumeration object to find the address of the memory space allocated to it. The piece of code in Program 40 illustrates the application of the address-of operator on an enumeration object.
      Program 40. A program that illustrates the use of the address-of operator on an object of enumeration type
    3. Assignment of an enumeration object to an enumeration variable: A variable of an enumeration type can be assigned with another object of the same enumeration type or with one of the enumerators present in the enumeration list or with an integer value.
    4. Behavior of equality operator on the objects of an enumeration type: The equality operator can be applied to check the equality of two objects of an enumeration type.
      The piece of code in Program 41 illustrates the use of an assignment operator and the equality operator on the objects of an enumeration type.
      Program 41. A program that illustrates the use of the equality operator on the objects of an enumeration type
    5. Other operators: All the operators that work on integer objects can be applied on the objects of an enumeration type and those that can be applied on integer constants can be applied on enumerators. The piece of code in Program 42 illustrates the use of logical operators on the objects of enumeration types.
      Program 42. A program that illustrates the use of logical operators on the objects of the enumeration type
    6. Type conversions: The objects of the enumeration type can participate in the expressions and can be passed as arguments to functions. Whenever necessary, an enumeration type is automatically promoted to an arithmetic type. The piece of code in Program 43 illustrates this fact.
      Program 43. A program that illustrates the implicit-type conversion of an enumeration object
    7. Limitation of enumeration type: The only limitation of an enumeration type is that it is not possible to print the value of an enumeration object in the symbolic form. The value of an enumeration object is always printed in the integer form. However, a debugger may be able to print the values of enumeration objects in the symbolic form. The piece of code in Program 44 illustrates this fact.
      Program 44. A program illustrating that the value of an enumeration object cannot be printed in the symbolic form

Post a Comment

Previous Post Next Post