Enumeration in C programming with Examples

 

1.It is a collection of named integer constant.
2.It is a user defined data type.
3.enum keyword is used to create a enumeration.

4.Default value of enum's elements are starts with 0 for example suppose that there are four elements in enum like sunday , monday , tuesday and wednesday then here value of sunday is 0, monday is 1, tuesday is 2 and wednesday is 3.
5.But we can also change the default value of enum's element which we discuss later in this post.

Syntax

Example 

enum week {sunday,monday,tuesday,wednesday,thrusday,friday,saturday};
1.enum is a keyword.
2.week is the name of enum and it is a user defined data type.
3.sunday,monday,tuesday,wednesday,thrusday,friday,saturday are the values of enum.
Default value of enum's member
Default value of sunday is 0
Default value of monday is 1
Default value of tuesday is 2
Default value of wednesday is 3
Default value of thrusday is 4
Default value of friday is 5
Default value of saturday is 6
For better understanding see the below example.

 
#include<stdio.h>
enum week {sunday,monday,tuesday,wednesday,thrusday,friday,saturday};
int main()
{
enum week obj;
obj=wednesday;
printf("Value of wednesday=%d",obj);
}
/*
### Output ###
Value of wednesday=3
*/

We can also change the default value of member of enum. See the example below

#include<stdio.h>
enum Age {Ravi=20,Shyam=25,Rocky=18,Daniel=22,Lina=16};
int main()
{
enum Age student;
student=Lina;
printf("Age of Lina is %d",student);
}
/*
### Output ###
Age of Lina is 16
*/

Here I am requesting if you found this post was helpful for you then let me know by your comment in below comment box and share it with your friends. Also you can contact me on Instagram @rajusunagar_

If you have not subscribed my website then please subscribe my website. Try to learn something new and teach something new to other. 

Thank you!!.......


Post a Comment

0 Comments