This blog is under construction

Saturday 19 May 2012

enumerated data type in C

Enumeration is user defined data type used to store integer values.  enum is the keyword used for declaring enumeration data type.  Enumeration values are signed integer by default.  User can create his own enumerated data type and associate a specific set of values to it.

How to define enumeration?

Consider the following example,
enum vibgyor {
                         VIOLET,
                         INDIGO,
                         BLUE,
                         GREEN,
                        YELLOW,
                        ORANGE,
                        RED
                       };

Here, vibgyor is an enumeration which contains 7 values.  By default, the values of VIOLET, INDIGO, BLUE, GREEN, YELLOW, ORANGE and RED are 0, 1, 2, 3, 4, 5, 6, 7 correspondingly.

User can explicitly set values for the constant names in enumeration list as shown below.
enum vibgyor {
                         VIOLET = 100,
                          INDIGO,
                          BLUE,
                          GREEN,
                         YELLOW,
                         ORANGE,
                         RED
                       };

Now, the values of integer constants VIOLET, INDIGO, BLUE, GREEN, YELLOW, ORANGE, RED are 100, 101, 102, 103, 104, 105, 106 correspondingly.

enum vibgyor {
                         VIOLET,
                          INDIGO,
                          BLUE,
                          GREEN = 100,
                         YELLOW,
                         ORANGE,
                         RED
                       };


Here, the values of integer constants VIOLET, INDIGO, BLUE, GREEN, YELLOW, ORANGE, RED are 0, 1, 2, 100, 101, 102, 103 correspondingly.

How to declare enumerations?
Enumeration variables can be declared while defining enumeration or after defining enumeration.

enum vibgyor {
                         VIOLET,
                          INDIGO,
                          BLUE,
                          GREEN = 100,
                         YELLOW,
                         ORANGE,
                         RED
                       } color;


Here, enumeration variable is declared while defining the enumeration.  color is the variable of type enumeration vibgyor.

Below is another method of declaring enumeration where enumeration variable is declared after defining the enumeration vibgyor.

enum vibgyor {
                         VIOLET,
                          INDIGO,
                          BLUE,
                          GREEN = 100,
                         YELLOW,
                         ORANGE,
                         RED
                       };

enum vibgyor color;

Here, color is an enumeration variable declared after defining the enumeration vibgyor.

How to initialize enumeration?
Lets see how to assign values to enumeration variable.  Consider the following,

enum vibgyor {
                         VIOLET,
                          INDIGO,
                          BLUE,
                          GREEN = 100,
                         YELLOW,
                         ORANGE,
                         RED
                       };
enum vibgyor color = VIOLET;

Here, we have assigned VIOLET to enumeration variable color.  So, the value of color would be 0(because value of VIOLET is 0).  These enumeration values can be assigned to any integer variables.


enum data type example in C:

  #include <stdio.h>
  /* defining enumeration */
  enum vibgyor {VIOLET, INDIGO, BLUE, GREEN,
                       YELLOW = -10, ORANGE, RED = 6};

  int main() {
        int i;
        enum vibgyor arr[7];  // declaring an array of enumeration vibgyor
        /* initialization for enumerated variables */
        arr[0] = VIOLET, arr[1] = INDIGO;
        arr[2] = BLUE, arr[3] = GREEN, arr[4] = YELLOW;
        arr[5] = ORANGE, arr[6] = RED;

        /* printing the values in reverse order*/
        for (i = RED; i >= 0; i--) { // assigned enumeration value to integer i
                printf("Value of arr[%d]: %d\n", i, arr[i]);
        }
        return 0;
  }

  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Value of arr[6]: 6
  Value of arr[5]: -9
  Value of arr[4]: -10
  Value of arr[3]: 3
  Value of arr[2]: 2
  Value of arr[1]: 1
  Value of arr[0]: 0



1 comment:

  1. Dell Laptop Repair Center in Noida is no.1 service center which provides door to door services in or its nearby areas. We have expert, technicians who can repair your laptop at your home. . Call us: 9891868324

    ReplyDelete