Enum(Enumeration)
Enumeration is a user defined datatype in C language.
primry data types
==============
int
float
double
char
long int
short int
unsigned
Derived data type
===============
Array, String
User defined data types
=================
Structure, Union, Enum
It is used to assign names to the integral constants which makes a program easy
to read and maintain.
The keyword “enum” is used to declare an enumeration.
Here is the syntax of enum in C language,
enum enum_name {
const1, const2, .......
};
The enum keyword is also used to define the variables of enum type. There are two
ways to define the variables of enum type as follows.
enum week{sunday, monday, tuesday, wednesday, thursday, friday,
saturday};
enum week day;
Here is an example of enum in C language,
Example
#include<stdio.h>
enum week{Mon=10,Tue,Wed,Thur,Fri=10,Sat=16,Sun};
enum day{Mond,Tues,Wedn,Thurs,Frid=18,Satu=11,Sund};
int main(){
printf("The value of enum week: %d\t%d\t%d\t%d\t%d\t%d\t%d\n\
n",Mon,Tue,Wed,Thur,Fri,Sat,Sun);
printf("The default value of enum day: %d\t%d\t%d\t%d\t%d\t%d\t
%d",Mond,Tues,Wedn,Thurs,Frid,Satu,Sund);
return0;
Output
The value of enum week: 10 11 12 13 10 16 17
The default value of enum day: 0 1 2 3 18 11 12
--------------------------------
In the above program, two enums are declared as week and day outside the main()
function. In the main() function, the values of enum elements are printed.
// An example program to demonstrate working
// of enum in C
#include<stdio.h>
enumweek{Mon, Tue, Wed, Thur, Fri, Sat, Sun};
intmain()
{
enumweek day;
day = Wed;
printf("%d",day);
return0;
}
Output:
2
// Another example program to demonstrate working
// of enum in C
#include<stdio.h>
enumyear{Jan, Feb, Mar, Apr, May, Jun, Jul,
Aug, Sep, Oct, Nov, Dec};
intmain()
inti;
for(i=Jan; i<=Dec; i++)
printf("%d ", i);
return0;
Output:
0 1 2 3 4 5 6 7 8 9 10 11
https://www.geeksforgeeks.org/enumeration-enum-c/