The perfect place for easy learning...

C Programming Language

×

Topics List

Place your ad here

Enumerated Types (enum) in C

In C programming language, enumeration is used to create user defined datatypes. Using enumeration, integral constants are assigned with names and we use these names in program. Using names in programming makes it more readable and easy to maintain.

Enumeration is the process of creating user defined datatype by assigning names to integral constants

We use the keyword enum to create enumerated datatype. The general syntax of enum is as follows...

enum {name1, name2, name3, ... }

In the above syntax, integral constant '0' is assigned to name1, integral constant '1' is assigned to name2 and so on. We can also assign our own integral constants as follows...

enum {name1 = 10, name2 = 30, name3 = 15, ... }

In the above syntax, integral constant '10' is assigned to name1, integral constant '30' is assigned to name2 and so on.

Example Program for enum with default values

#include<stdio.h>
#include<conio.h>

enum day {monday, tuesday, wednesday, thursday, friday, saturday, sunday} ;

void main(){
   
   enum day today;
   
   today = tuesday ;
   
   printf("\ntoday = %d ", today) ;
   
}

In the above example program a user defined datatype "day is created seven values, monday as integral constant '0', tuesday as integral constant '1', wednesday as integral constant '2', thursday as integral constant '3', friday as integral constant '4', saturday as integral constant '5' and sunday as integral constant '6'. Here, when we display tuesday it displays the respective integral constant '1'.

We can also change the order of integral constants, consider the following example program.

Example Program for enum with changed integral constant values

#include<stdio.h>
#include<conio.h>

enum day {monday = 1, tuesday, wednesday, thursday, friday, saturday, sunday} ;

void main(){
   
   enum day today;
   
   today = tuesday ;
   
   printf("\ntoday = %d ", today) ;
   
}

In the above example program, the integral constant value starts with '1' instead of '0'. Here, tuesday value is displayed as '2'.

We can also create enum with our own integral constants, consider the following example program.

Example Program for enum with defferent integral constant values

#include<stdio.h>
#include<conio.h>

enum threshold {low = 40, normal = 60, high = 100} ;

void main(){
   
   enum threshold status;
   
   status = low ;
   
   printf("\nYou are at %d state. Work hard to improve!!", status) ;
   
}

Some times we may assign our own integral constant from other than first name. In this case compiler follows default integral constants for name that are before the user defined integral constant and from user defined constant onwords it resumes. Consider the following example program.

Example Program for enum with changed integral constant values

#include<stdio.h>
#include<conio.h>

enum day {monday, tuesday, wednesday, thursday = 10, friday, saturday, sunday} ;

void main(){
   
   enum day today;
   
   today = tuesday ;
   
   printf("\ntoday = %d ", today) ;
   
   today = saturday ;
   
   printf("\ntoday = %d ", today) ;
   
}

In the above example program a user defined datatype "day is created seven values, monday as integral constant '0', tuesday as integral constant '1', wednesday as integral constant '2', thursday as integral constant '10', friday as integral constant '11', saturday as integral constant '12' and sunday as integral constant '13'.

Note - In enumeration, more than one name may given with same integral constant.


Place your ad here
Place your ad here