Introduction:
- An enumeration consists of a set of named integer constants.
- Each enumeration-constant in an enumeration-list names a value of the enumeration set.
Syntax:
enum identifier { enumerator-list }; |
By default, the first enumeration-constant is associated with the value 0.
enum colors { black, red, green, blue, white }; int main() { int i; enum colors c; for(i=0 ; i<5 ; i++) { printf(“%d \n”, c=i); } return 0; } |
If we don’t set values explicitly, by default values set in increasing order by one:
#include<stdio.h> int main() { enum months {Jan=1, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec}; enum months month; printf(“month=%d\n”,month=Feb); return 0; } |
By using constant integer values, we can use the functionality of enum set elements:
#include<stdio.h> enum Days { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday }; int main() { enum Day today; int x; printf(“Enter Day of Week(0 to 6) : “); scanf(“%d”,&x); today=x; if(today==Sunday || today==Saturday) printf(“Enjoy! Its the weekend\n”); else printf(“Week day – do your work\n”); return 0; } |