Enumeration, a special keyword in C, is a user-defined data type that stores integer constants. It’s a powerful tool that enhances code readability and maintainability. In this article, we will explore the concept of enumeration in the C programming language.
What is Enumeration or Enum in C
It is a special keyword in the C language. That is used for storing the integer constants in C language. Enum in C language is used to write clean, easy to read and easy-to-maintainable code.
The following image demonstrates the enum keyword:
The enum is used to create an enumerated data type in C language. The following program demonstrates the same:
enum Employee{
Name,
Salary,
joingin_date,
};
In the above code, the Employee is the data type in C language, and Name, Salary, and Joining_date are different enum_names separated by a comma.
Get curriculum highlights, career paths, industry insights and accelerate your technology journey.
Download brochure
Syntax of enum in C
The syntax for defining an enumeration in C is straightforward. Here, I am defining the syntax of the C language.
enum: This keyword is used to define the enumeration
Enumeration_name: It is the name of the enumeration type.
enumerator1, enumerator2: This is the named constants representing integer values. These names are optional and can be omitted.
The following program example of the Enumeration
enum Color {
RED,
GREEN,
BLUE
};
enum Color colorSelect = GREEN;
In the above example, colorSelect is a variable of type enum Color, and It is assigned the value of Green in this variable, which is equivalent to 1. We can use these symbolic names for better readability of code and maintainability in your source code.
Examples of enum declaration
In the C language, we can declare the enumerator type in two different ways. Let’s examine the two different ways of defining the enum declaration.
The first enum name is value by default assigned 0. If it is not initialised. Then, the next enum names are assigned by an increment of 1 and so on. The following program demonstrates the enum increment initialization.
Progarm
#include <stdio.h>
// declaration on enum
enum Weeks {
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday
};
int main() {
// Defining the variable of type enum
enum Weeks days = Monday;
printf("Selected Days are %dn", days);
days = Tuesday ;
printf("Selected Days are %dn",days) ;
days = Wednesday ;
printf("Selected Days are %dn",days) ;
return 0;
}
Output
Selected Days are 0
Selected Days are 1
Selected Days are 2
Initialising the values
In this section, we will assign the enum names, and the next enum names follow the same increment pattern 1. For example
enum Colors{
RED = 20,
GREEN = 30,
BLUE
}
In the above program, RED and GREEN have 20 and 30 values, respectively, initialised. The value of Blue is 31 because every element in the enum takes the next integer value of its previous if it is not initialised.
The following program demonstrates the initialising of the values.
Program
#include <stdio.h>
// declaration on enum
enum Colors {
RED = 10,
GREEN = 30,
BLUE
};
int main() {
// Initializing enum variable
enum Colors color = RED;
printf("Selected Color is %dn", color);
color = GREEN ;
printf("Selected Color is %dn",color) ;
color = BLUE;
printf("Selected Color is %dn", color);
return 0;
}
Output
Selected Color is 10
Selected Color is 30
Selected Color is 31
Defining enum variables by their integer equivalent values
In this section, we will initialise the integer value in the enum variable. The following program demonstrates the integer initialization.
Program
#include <stdio.h>
// declaration on enum
enum Color {
RED = 5,
GREEN = 9,
BLUE
};
int main() {
// Initializing enum variable
enum Color selected = RED;
printf("Selected feature is %dn", selected);
selected =343;
printf("Selected feature is %dn", selected);
return 0;
}
Output
Selected feature is 5
Selected feature is 343
In the above example, we directly assigned 343 to a colour variable in the RED variable.
Initialising the same values
We can initialise the same values to multiple enum variables. For example The following enum declares the same value in multiple variables.
Program
#include <stdio.h>
enum car {
run = 1,
brake = 0,
stop = 0
};
int main(){
enum car functions ;
functions = run ;
printf("Car Information = %dn",functions) ;
functions = brake ;
printf("Car Information = %dn", functions) ;
functions = stop ;
printf("Car Information = %dn",functions) ;
return 0 ;
}
Output
Car Information = 1
Car Information = 0
Car Information = 0
Utilising switch/case statements with an enum
In this section, we will use enum with switch case statements. Enum is good for defining the cases so that it becomes easy to modify the code later.
The following program demonstrates the switch case statement.
Program
#include <stdio.h>
// declaration on enum
enum Color {
RED = 1,
GREEN = 2,
BLUE = 3
};
int main() {
// Initializing enum variable
enum Color color = RED;
switch (color) {
case 1:
printf("It is Red");
break;
case 2:
printf("It is Green");
break;
case 3:
printf("It is UNDERLINE");
}
return 0;
}
Output
Car Information = 1
Car Information = 0
Car Information = 0
Enum with the conditional operator
In this section, we will implement an enum with the conditional operator. The following program demonstrates the enum with the conditional operator.
Program
#include <stdio.h>
enum Color {RED = 0, GREEN, BLUE};
enum Color currState = 0;
enum Color FindColor() {
return currState;
}
int main() {
(FindColor() == RED)? printf("Color is Red"): printf("Please Choose Right Color");
return 0;
}
Output
Color is Red
Enum vs Macros
The following table differentiates the Enum vs Macros in tabular form.
Feature
Enumerations
Macros
Purpose
It is defined integer constants
It defines symbolic constants or code snippets
Type Safety
Provide type safety
Lacks type safety
Readability
Enhances code readability
My reduced code readability, if not used regularly
Debugging
Easy to debug due to named constants
It is harder to debug due to the replacement of values.
Code Generation
Suitable for representing related concepts
It is suitable for code generation or replacement during preprocessing.
Conclusion
In this article, we have learned about enumeration in C language. It is a powerful feature that allows programmers to define their own data types consisting of a set of named constants known as enumerations. Enumerations enhance code readability, maintainability, and type safety by replacing hard-to-remember numeric constants with meaningful names or variables. By understanding enumerations, developers can create self-documenting code that communicates the intention and purpose of the constants more effectively.
FAQs
What is an enumeration in C language?
An enumeration in C is a user-defined data type that consists of a set of named constants known as enumerators, which represent integer values.
How do I define an enumeration in C language?
To define an enumeration in C language, We can use the ‘enum’ keyword followed by the enumeration name and a list of enumerator names enclosed in curly braces with enumeration variables.
Can I use enumerations in switch statements?
Yes, we can also use enumeration in switch statements in the C language. You can use enumeration as case labels to specify different actions based on the value of an enumeration variable.
Can I convert enumeration values to integers in C?
There are two types of conversion in C: implicit and explicit. We can use this conversion type to convert enumeration values to integers in C. Enumeration values are essentially integer constants, so you can perform arithmetic operations or assign them to integer variables.
Hero Vired is a leading LearnTech company dedicated to offering cutting-edge programs in collaboration with top-tier global institutions. As part of the esteemed Hero Group, we are committed to revolutionizing the skill development landscape in India. Our programs, delivered by industry experts, are designed to empower professionals and students with the skills they need to thrive in today’s competitive job market.