C Enumeration (enum)
What is Enumeration (enum) in C?
An enumeration (enum) in C is a user-defined data type that consists of a set of named integer constants.
It improves code readability by allowing meaningful names to represent integer values instead of using numeric constants directly.
By default, the first enumerator has the value 0, and each subsequent enumerator is assigned the next integer value automatically unless specified otherwise.
Example
enum Day
{
Sunday,
Monday,
Tuesday,
Wednesday
};
Explanation
• enum is the keyword used to define an enumeration.
• Day is the name of the enumeration.
• Sunday, Monday, Tuesday, and Wednesday are enumerators.
• By default, Sunday = 0, Monday = 1, Tuesday = 2, and Wednesday = 3.
• Enumerations make programs easier to read and maintain.
Why is Enumeration (enum) Important?
Enumeration (enum) is important because it:
• Improves code readability.
• Replaces numeric constants with meaningful names.
• Reduces programming errors.
• Makes programs easier to maintain.
• Is widely used to represent fixed sets of values.
Types of Enumeration Usage in C
Enumerations can be used in different ways depending on the programming requirement.
The most common uses of enumerations are:
• Simple Enumeration
• Enumeration with Assigned Values
• Enumeration Variables
These usages make programs more readable and easier to manage.
Simple Enumeration
A simple enumeration assigns integer values automatically, starting from 0.
Each enumerator receives the next integer value unless a specific value is assigned.
Example
enum Color
{
Red,
Green,
Blue
};
Explanation
• Red is assigned the value 0.
• Green is assigned the value 1.
• Blue is assigned the value 2.
• Values are assigned automatically in sequence.
Enumeration with Assigned Values
You can assign custom integer values to enumeration constants.
If a value is assigned to one enumerator, the following enumerators continue from that value unless specified otherwise.
Example
enum Status
{
Success = 1,
Warning = 5,
Error = 10
};
Explanation
• Success is assigned the value 1.
• Warning is assigned the value 5.
• Error is assigned the value 10.
• Custom values improve program flexibility.
Enumeration Variables
An enumeration variable is declared using the enum data type.
It can store only the values defined in the corresponding enumeration.
Example
enum Day
{
Sunday,
Monday,
Tuesday
};
enum Day today = Monday;
Explanation
• today is an enumeration variable.
• It stores one of the values defined in enum Day.
• Here, today stores the value Monday.
• Enumeration variables make the program more meaningful.
Advantages of Enumeration
• Improves code readability.
• Replaces numeric constants with meaningful names.
• Reduces programming errors.
• Makes programs easier to maintain.
• Organizes fixed sets of related values efficiently.
Example Program
Program
#include <stdio.h>
enum Day
{
Sunday,
Monday,
Tuesday,
Wednesday
};
int main()
{
enum Day today = Tuesday;
printf(“Today’s value = %d”, today);
return 0;
}
Output
Today’s value = 2
Explanation
• enum Day defines four named constants.
• Enumeration values start from 0 by default.
• Tuesday automatically gets the value 2.
• today is an enumeration variable.
• printf() displays the integer value of the enumeration constant.
Key Points
• Enumeration (enum) is a user-defined data type.
• It stores a fixed set of named integer constants.
• Enumeration values start from 0 by default.
• Custom values can also be assigned.
• Enumerations improve code readability.
• They reduce the use of numeric constants.
• Enumerations make programs easier to maintain.
