Enum or Enumeration In C Language [With Examples]
There are three ways to create User Defined Data Types in C language – Structure, Union, and Enum.
In today’s article, we are going to talk about Enum or Enumeration. Today we will know in detail about, what is Enum In C Language. And how to use Enum in C language?
So without wasting time let’s first understand what is Enum in C language.
What is Enum or Enumeration?
Enum (Enumeration) is a user-defined data type, used mainly in C language to assign names to integral constants.
Through enum (Enumeration), we give a meaningful name to integral constants. This makes the program easy to read and easy to maintain.
The enum keyword is used to define enumeration. Its basic syntax is something like this –
Basic Syntax of enum in C language,
enum enum_name {const1, const2, const3....... };
- Here enum is a keyword with the help of which we are defining a new data type.
- enum_name is the name of the new data type. You can keep this name as per your wish.
- The value of const1 is 0 by default and after that the value of const2, const3 goes on doing 1, 2, 3 like this, you can change these by default values at the time of declaration only if needed.
Example -:
// Changing default values of enum constants
enum month { jan = 1, feb, march ....... };
In this example, we changed the value of jan to 1, which is 0 by default.
Enumerated Type Declaration In C
If you have read the structure, then you must have seen that just by defining the structure, the structure does not get any space in the memory. Structure gets space in memory when we declare variable of that structure.
Similarly, after creating the Enumerated data type, we have to declare its variable.
Let us learn how to declare a variable with an enumerated type.
Declaration of enum variable
In C language, we can declare a variable with enumerated type in two ways -:
- By enum keyword inside the main() function
- At the end of the enumeration itself while defining the enumeration.
Method 1 -: By enum keyword inside main() function
Let’s understand this better through an example.
Example -:
#include<stdio.h>
enum month
{ January, February, March, April, May, June, July, August, September, October, November, December
};
void main()
{
enum month m1, m2;
}
Method 2 -: At the end of the enumeration while defining the enumeration.
Let’s understand this also through an example.
Example -:
enum month
{ January, February, March, April, May, June, July, August, September, October, November, December
}m1, m2;
Note -: In enum variable, we can assign or store only one value at a time
How to Use Enum
In C language we use Enum in this way -:
Example -:
#include<stdio.h>
enum month
{ January = 1, February, March, April, May, June, July, August, September, October, November, December
};
void main()
{
enum month m1, m2;
m1 = January;
m2 = February;
printf("Month %d", m1);
printf("\nMonth %d", m2);
}
In this example, we have created a new data type named month which holds the details of all the months. Since enum variables can store only one value, by assigning January to m1 and February to m2, the values of m1 and m2 are printed by the printf function.
Its output will be something like this –
Output -:
Month 1
Month 2
Why we use enum
When we want our variable to have only a set of values, then we use enum. like rambow has only 7 colors so here we can use enum to represent these seven colors.
An enum is used to store a value, that can never change, such as month names, seven days of the week, true, false, etc.
Enum is mostly used with switch-case statements because in switch-case statements only one statement is executed at a time.
The use of enum increases the readability of the program
Example -:
#include<stdio.h>
enum boolean
{ false , true
}result;
void main()
{
int x;
printf("Enter a number ");
scanf("%d", &x);
if(x%2==0)
result = true;
else
result = false;
if (result)
printf("Even Number");
else
printf("Odd Number");
}
In this example, we created a boolean and created an enum variable named result. In this program, we took a value from the user and applied an if condition to find out whether this value is an odd number or an even number, and assigned the result to the result variable.
According to whatever value came in this result variable, we printed that result on the screen.
Its result was something like this –
Output -:
Enter a number
15
Even number
Important Point About Enum or Enumeration
- If we will not assign any value to the enum name then the compiler by default assigns 0 to the initial name. and then assigns the value incrementing the name by 0
- You can change the default value of enum name at the time of declaration as per your requirement.
- We can declare enum name in any order.
- The enum names created in the same scope should not be same, otherwise compiler gives error.
Read More -:
- String in C
- Structure in C
- Union in C
- Typedef in C
- Pointer in C
- File Handling in C
- Preprocessor In C
- Dynamic Memory Allocation in C
Conclusion
Friends, I hope you have found the answer to your question and you will not have to search about What is Enum In C? How to use Enum in C language?
However, if you want any information related to this post or related to programming language, or computer science, then comment below I will clear your all doubts
If you want a complete tutorial on C language, then see here C Language Tutorial. Here you will get all the topics of C Programming Tutorial step by step.
Friends, if you liked this post, then definitely share this post with your friends so that they can get information about Enumeration or Enum In C
To get the information related to Programming Language, Coding, C, C ++, subscribe to our website newsletter. So that you will get information about our upcoming new posts soon.