In this blog, you will learn what enumeration is and its implementation of code in the C programming language. This blog will guide you on when and how to use enumeration in C, including implementation in switch statements and flags. Further, we will be exploring the differences between enum and macro.
Table of Content
Learn the fundamentals of C by watching the video below:
What is Enum in C?
In C programming, an enum (enumeration) is a user-defined data type that is used to define a set of named integral constants; these constants are also known as enumerators. Enums allow you to create symbolic names (identifiers) that represent a set of values of different types, for example, integers, characters, floats, etc.
Syntax for Declaring Enum in C
In C, you can declare an enumeration using the ’enum’ keyword, followed by the name of the enumeration and the list of constants inside curly braces. Here’s an example:
// Declaration of an enumeration named Color
enum Color {
RED,
GREEN,
BLUE
};
In this example, RED, GREEN, and BLUE are constants within the ‘Color’ enumeration. By default, they are assigned values starting from 0 and incremented by 1 for each subsequent constant. You can also explicitly assign values to these constants as well.
Let’s understand this with one example:
// Enumeration with specially assigned values
enum Status {
OK = 1,
ERROR = -1,
IN_PROGRESS = 0
};
In the above declaration, we have created an enumeration named Status where OK is assigned the value 1, ERROR is assigned -1, and IN_PROGRESS is assigned 0.
Learn C in detail with the C Programming Certification Course!
Enumerated Type Declaration to Create a Variable
In C, while declaring an enumeration, you can create variables of that enumerated type using the enum name that you have defined. Let’s understand this with one example:
// Declaration of an enumeration named Color
enum Color {
RED,
GREEN,
BLUE
};
To create a variable of type Color, you just have to use the enum name followed by the variable name:
// Creating a variable of type Color
enum Color chosenColor; // This declares a variable named chosenColor of type Color
After declaring chosenColor of type Color, you can assign any value from the Color enumeration to it:
chosenColor = RED; // Assigning the value RED to chosenColor
This assigns the value RED from the Color enumeration to the variable chosenColor.
Take the first step to become a programming master with our C Programming Tutorial today!
Implementation of Enum in C Program
Let’s understand how we can implement the enumeration inside our code in the C language:
#include <stdio.h>
// Declaration of an enumeration named Color
enum Color {
RED,
GREEN,
BLUE
};
int main() {
// Creating a variable of type Color
enum Color chosenColor = BLUE;
// Using the enum variable in a simple conditional statement
if(chosenColor == RED) {
printf("The chosen color is Red.\n");
} else if(chosenColor == GREEN) {
printf("The chosen color is Green.\n");
} else if(chosenColor == BLUE) {
printf("The chosen color is Blue.\n");
} else {
printf("Unknown color.\n");
}
return 0;
}
When to Use Enum in C
Enums in C are used to define a set of named integral constants that represent a set of related values. Here are some situations where enums are commonly used:
- Improving Code Readability: Enums make code more readable by providing descriptive names for integral constants. For example, instead of using numeric values directly, you can use enum constants like RED, GREEN, and BLUE for colors, enhancing code readability and maintainability.
- Switching Between States: Enums are commonly used in switch-case statements to execute different blocks of code based on the value of an enum variable. This usage is especially helpful when dealing with different states or options within a program.
- APIs and Interfaces: Enums are useful while defining APIs or interfaces between different parts of a program. They provide a clear and structured way to communicate specific options or states between different modules or functions.
- Flags and Bitmask Operations: Enums can also be used with bitwise operations to represent flags or bitmasks, where each enum constant represents a bit or a combination of bits of individual value.
Crack your next interview with the help of our C and Data structure interview Questions and Answers!
Utilizing Switch Statements with Enum
Switch statements are commonly used with enums in C to execute different blocks of code based on the value of an enum variable. Here’s an example that will help you understand the usage of switch statements with enums:
#include <stdio.h>
// Declaration of an enumeration named Day representing days of the week
enum Day {
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
SUNDAY
};
int main() {
enum Day today = WEDNESDAY;
switch(today) {
case MONDAY:
printf("It's Monday. Work mode ON!\n");
break;
case TUESDAY:
printf("It's Tuesday. Keep up the pace!\n");
break;
case WEDNESDAY:
printf("It's Wednesday. Halfway there!\n");
break;
case THURSDAY:
printf("It's Thursday. Almost done!\n");
break;
case FRIDAY:
printf("It's Friday. Weekend is coming!\n");
break;
case SATURDAY:
printf("It's Saturday. Time to relax!\n");
break;
case SUNDAY:
printf("It's Sunday. Prepare for the week ahead!\n");
break;
default:
printf("Invalid day selected.\n");
}
return 0;
}
In the above example, the enum ‘Day’ represents days of the week. The program initializes ‘today’ with the value ‘WEDNESDAY’ and uses a ‘switch’ statement to determine the day and print a message based on the value of ‘today’, which means depending on the value of the ‘today’ variable, the corresponding case block will be executed and will print a message for that day. If the value doesn’t match any case, the default block will be executed, and it will print “Invalid day selected.”
Using Enums for Flags
Enums are often used in C to create sets of flags or bitmasks by assigning different values to each constant that allow the combination of these values for various options or states. We can implement this by using bitwise operations to set, unset, or check multiple flags within a single integer variable. Here is an example demonstrating how enums can be used as flags:
#include <stdio.h>
// Declaration of an enumeration named Permissions as flags
enum Permissions {
READ = 1, // Binary: 0001
WRITE = 2, // Binary: 0010
EXECUTE = 4 // Binary: 0100
};
int main() {
// Creating a variable to hold multiple permissions
enum Permissions userPermissions = READ | WRITE; // Binary: 0011 (READ | WRITE)
// Checking for specific permissions using bitwise AND
if (userPermissions & READ) {
printf("User has READ permission.\n");
}
if (userPermissions & WRITE) {
printf("User has WRITE permission.\n");
}
if (userPermissions & EXECUTE) {
printf("User has EXECUTE permission.\n");
} else {
printf("User doesn't have EXECUTE permission.\n");
}
// Revoking a permission using bitwise NOT and AND
userPermissions &= ~WRITE; // Removes WRITE permission
printf("\nAfter revoking WRITE permission:\n");
// Checking permissions after modification
if (userPermissions & READ) {
printf("User still has READ permission.\n");
}
if (userPermissions & WRITE) {
printf("User still has WRITE permission.\n");
} else {
printf("User doesn't have WRITE permission anymore.\n");
}
if (userPermissions & EXECUTE) {
printf("User has EXECUTE permission.\n");
} else {
printf("User doesn't have EXECUTE permission.\n");
}
return 0;
}
In this example, the ‘Permissions’ enum represents individual permissions as flags: ‘READ’, ‘WRITE’, and ‘EXECUTE’. Bitwise ‘OR’ (‘|’) is used to combine ‘READ’ and ‘WRITE’ permissions for the ‘userPermissions’ variable. Bitwise ‘AND’ (‘&’) is used to check if a specific permission is present, and bitwise NOT (‘~’) is used in combination with AND to revoke permission (WRITE, in this case).
Difference Between Enum and Macro
We use enums and macros for different purposes in C programming. Enums create symbolic names for related integral constants that give better readability, type safety, and values with scope in the enumeration. Enums are primarily used for defining a limited set of related options or states. On the other hand, macros are defined with ‘#define’ that perform text substitution globally, enabling versatile usage for constants but lacking in type safety and scoping. While enums enhance code clarity and maintainability by associating names with values, macros offer flexibility, but there are readability issues and unexpected behavior due to their direct textual replacement nature.
Example of Enum
#include <stdio.h>
// Declaration of an enumeration named Color
enum Color {
RED,
GREEN,
BLUE
};
int main() {
enum Color chosenColor = GREEN;
switch(chosenColor) {
case RED:
printf("The chosen color is Red.\n");
break;
case GREEN:
printf("The chosen color is Green.\n");
break;
case BLUE:
printf("The chosen color is Blue.\n");
break;
}
return 0;
}
Example of Macro :
#include <stdio.h>
// Macro definition for maximum value
#define MAX(a, b) ((a) > (b) ? (a) : (b))
int main() {
int num1 = 10, num2 = 20;
int maximum = MAX(num1, num2);
printf("The maximum of %d and %d is %d\n", num1, num2, maximum);
return 0;
}
In the above example, ‘enum Color’ defines symbolic constants for colors that are easy to read and type safely. The example of the functionality of macro defines ‘MAX(a, b)’ to find the maximum of two values. Macros perform text substitution directly by replacing ‘MAX(a, b)’ with the corresponding expression, but that does not provide type safety and scoped values.
In the enum case, ‘chosenColor’ is a variable of type ‘enum Color’ that can only hold the values ‘RED’, ‘GREEN’, or ‘BLUE’. Whereas the ‘MAX’ macro provides a flexible way to find the maximum value of any two inputs but doesn’t give type safety or scoping.
Conclusion
Enums in C provide a structured way to define named integral constants that enhance code readability, maintainability, and type safety. They associate symbolic names with related values, making the code more understandable. Unlike macros, enums offer scoped constants that reduce errors and improve code clarity, especially when defining a limited set of related options or states.