Enumeration in C

by | Aug 22, 2022 | C

Home » C » Enumeration in C

Introduction

Recall the two very important topics of our C language series, which were data types and structure. int, char, float, and double are the predefined data types as discussed earlier.

Go through the chapter named ‘Structure’, where you will observe that the ‘struct’ keyword and the structure name are used as a data type to declare variables, functions, etc.

It means it is a type of data type that we are defining in our code to make things easier. So, it is important to note that structure is one of the user-defined data types.

Enumeration (enum) is another user-defined datatype (same as structure). No doubt we can write our programs without using defined such datatypes like structure and enum, but they make code simple, easy to understand, and reflects good programming skills.

Basically, it consists of some elements of that type. For example, C, C++, Java, and Python are the names of programming languages. Thus, we can say that these are the types of enumeration with names the ‘Languages’ and C, C++, Java, and Python as their elements.

Hopefully, you got the basic idea of enumeration. Now let’s see this topic in detail.

Defining an enum

The definition of enum is made with the keyword ‘enum’ along with its name, and the elements are separated by ‘,’ comma.

Syntax:

enum enum_name

{

element 1,

element 2,

element 3,

…….

element n,

};

 

For example

enum Languages

{

C++,

C,

Java,

Python

};

 

‘Languages’ is the name of the enum defined with the ‘enum’ keyword, and C, C++, Java, and Python are the elements separated by the comma.

Declaration of Enum variable

Declaration of the variables of type ‘enum’ is very simple and can be made in two ways.

Method 1

Syntax:

enum enum_name

{

element 1,

element 2,

element 3,

…….

element n,

};

main()

{

enum enum_name variable_name;

}

 

For example

enum Languages

{

C++,

C,

Java,

Python

};

main()

{

enum Languages L;

}

 

In the above code, firstly, we declare an enum named ‘Languages’ with its elements. Then in the ‘main’ function, we declare a variable name ‘L’ of type ‘enum Languages’.

Method 2

Syntax:

enum enum_name

{

element 1,

element 2,

element 3,

…….

element n,

} variable_name;

 

For example:

enum Languages

{

C++,

C,

Java,

Python }L;

 

The second way is also a simple one. In this, there is no need to declare the variable in the ‘main’ function along with its type. Just write the variable’s name after the curly braces, as shown in the above example.

Values of the members of the enum

This is something new for us. Till now, whatever we have studied was almost the same as structure. But it is interesting to note that all the members of ‘enum’ have a value.

By default, the value of the member written first is 0, 1 is the value of the second element, that of the third element is 2, and so on.

The example of enum discussed above have values to the members as shown below

Program:

enum Languages

{

C++,

C,

Java,

Python }L;

 

Members C++ C Java Python
Values 0 1 2 3

Values will be assigned according to the order of elements we maintain in our code. If member ‘python’ is written in the first place, then the value of ‘python’ will be 0. Since it is written last in the above code, its value is 3.

Program:

#include<stdio.h>

enum month

{

January,

February,

March,

April

};

int main()

{

enum month a,b,c,d;

a=January;

b=February;

c=March;

d=April;




printf("Value of member January is %d\n",a);

printf("Value of member Feburary is %d\n",b);

printf("Value of member March is %d\n",c);

printf("Value of member April is %d\n",d);

return 0;

}

 

Output:

In the above code, we defined an enum named ‘month’ and wrote its members in its body. After that, in the ‘main’ function, we declare four variables a,b,c, and d of type ‘enum’. Since we know the values of January, February, March, and April is 0,1,2,3.

Thus, by writing a=January; , b=February; , c=March; , d=April; we assign values 0,1,2,3 to the variables a, b, c, d respectively.

Moreover, it is also possible that we can assign any value to any element according to our choice. But this will alter the values of the elements written after that.

Look at the code written below to know more about this concept.

Program:

#include<stdio.h>

enum week

{

Sunday,

Monday=100,

Tuesday,

Wednesday,

Thursday,

Friday,

Saturday

};

int main()

{

enum week a,b,c;

a=Sunday;

b=Tuesday;

c=Wednesday;




printf("Value of member Sunday is %d\n",a);

printf("Value of member Tuesday is %d\n",b);

printf("Value of member Wednesday is %d\n",c);

return 0;

}

 

Output:

While defining the enum named ‘week’, we assign 100 as a value to the member ‘Monday’.

In the ‘main’ function, we declare three variables a, b, and c of type ‘enum week’.

Since we know the default value of the first member is 0, that of the second member is 1, and so on. That’s why on printing the value of ‘a’, we got 0 as output.

Now we expect the value of the members ‘Tuesday’ and ‘Wednesday’ as 2, 3 respectively, but we get something different in the output shown above.

This happens because we assign 100 to ‘Monday’. Thus, the members’ values after ‘Monday’ changed accordingly in a serial order.

Therefore, ‘Tuesday’ is 101, ‘Wednesday’ is 102, ‘Thursday’ is 103, ‘Friday’ is 104, and ‘Saturday’ is 105.

Program:

#include<stdio.h>

enum Alpha

{

A,

B,

C,

D,

};

int main()

{

enum Alpha x;

x=B;

printf("Value of member D is %d\n",x+2);

return 0;

}

 

Output:

The value of member ‘B’ is 1, and it is assigned to the variable ‘x’.

printf(“Value of member D is %d\n”,x+2); in this statement, we are printing the value of ‘x+2’ (=1+2), so the output we obtained is 3, which is the default value of member ‘D’.

Author

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Author