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.

POSTGRADUATE PROGRAM IN
Multi Cloud Architecture & DevOps
Master cloud architecture, DevOps practices, and automation to build scalable, resilient systems.
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 enumeration_name {
enumerator1,
enumerator2,
enumerator3,
};
Let’s broke the components of the enum syntax
- 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.
Declaration 1
enum week{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday,
}weeks;
In the above example of code, we declared the variable feature just after the braces,
Declaration 2
Here, we are declaring the Enumeration Variable.
enum week{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday,
}
int main(){
enum weeks ;
return 0;
}
Facts about the initialization of enum
The first value is, by default, 0
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 %d\n", days);
days = Tuesday ;
printf("Selected Days are %d\n",days) ;
days = Wednesday ;
printf("Selected Days are %d\n",days) ;
return 0;
}
Output
Selected Days are 0
Selected Days are 1
Selected Days are 2

82.9%
of professionals don't believe their degree can help them get ahead at work.
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 %d\n", color);
color = GREEN ;
printf("Selected Color is %d\n",color) ;
color = BLUE;
printf("Selected Color is %d\n", 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 %d\n", selected);
selected =343;
printf("Selected feature is %d\n", 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 = %d\n",functions) ;
functions = brake ;
printf("Car Information = %d\n", functions) ;
functions = stop ;
printf("Car Information = %d\n",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.
What is an enumeration in C language?
How do I define an enumeration in C language?
Can I use enumerations in switch statements?
Can I convert enumeration values to integers in C?
Updated on October 11, 2024
