ENUMERATION

 ENUMERATION


The enumerated data type is used when we know in advance about a finite list of values as a particular data type. The declaration of enumerated data type start with 'enum' keyword.. 'enum' keyword is followed by an enum name, an open brace, each of the values separated by a comma, and finally a closing brace and a semicolon. The list of values is known as the enumerated list of items. 

Syntax: -
enum <enum name> {value1, value2,value3.........value n};

 example:  enum color { red, blue, green, white, black);


 Note:- Here color is the name of the enum, and red, green, blue are the symbolic constants. Each enum constant has an integer value. If we have not given any value, then the initial value of the red is 0, blue is 1 and so on and Incremented by one. 


As shown in the example below:

#include<stdio.h>

#inciude<conio.h> 
enum col{red, green, blue, yellow,black,white};

void main()

{
printf("\n the value of red=%d",red);
printf("\n the value of green="%d",green); 
printf("\n the value of white=%d", white);
getch();
}

output:
the value of red=0 
the value of green=1 
the value of white=5

Initialization of enum constant


We can also change the value of the each constant in the enum. Any constant can be initialized with a particular value, and those that are not initialized will be incremented

automatically. 
As shown in the example below:

#include<stdio.h> 

#include<conio.h>
 enum col{red, green=5, blue, yellow, black, white};
 void main()
{
 clrscr();
 printf("\n the value of red=%d",red);
 printf("\n the value of green=%d",green);
 printf("\n the value of whitea%d" white);
 getch();
}

Output:
the value of red=0
the value of green=5 

the value of white=9 

0 comments:

Post a Comment