Showing posts with label Enumeration Types. Show all posts
Showing posts with label Enumeration Types. Show all posts

Sunday, January 10, 2016

Enumeration Types

Enumeration types are used to hold a value that belongs to a particular set. Keyword 'enum' is used to define enumeration types. Each element in the enumeration type is assigned with a consecutive integer constant starting with 0 by default.
Syntax for declaring enum is given below
enum type_name{value_1, value_2, value_3,...}
Each of the values in the value set (ie; value_1, value_2 etc) are integer constants. By default, they start with 0 and increments by 1. You can override this values your self. Let's take a look at it with the help of an example.
  1. #include <stdio.h>
  2. enum day_of_week{ sunday, monday, tuesday, wednesday, thursday, friday, saturday};
  3. int main(){
  4. enum day_of_week today;
  5. //Assigns enum value sunday to the variable today.
  6. today = sunday;
  7. printf("%d", today);
  8. return 0;
  9. }
  10.  
  11. Output:
  12. 0
In the above program enum type "day_of_week" consists of 7 values; Sunday to Saturday. Each of them is internally assigned with an integer constant starting with 0 by default. 'Sunday' gets 0, 'Monday' gets 1 and so on. Here the 'printf' statement will output the value 0, because we've assigned enum value 'Sunday' to the variable 'today' of type 'day_of_week'.