C Enumeration (or Enum)

What is 'enum' in C?

* Enums are commonly used in 'switch' and 'case' statements because they provide a convenient and readable way to handle multiple distinct cases or options in a program.

Enumeration

An enumeration, often referred to as an "enum," is a user-defined data type that consists of a set of named integer constants. Enums provide a way to create symbolic names for a list of related values, making the code more readable and maintainable.

Here's the basic syntax for defining an enumeration in C:

enum enumeration_name {
    constant1,
    constant2,
    constant3,
    /* ... */
};

Breakdown of mentioned enumeration syntax:

'enum': This keyword is used to define an enumeration.

'enumeration_name': This is the name you give to the enumeration, which is used as a data type.

'constant1', 'constant2', 'constant3', etc: These are the named constants (enumerators) that represent the values associated with the enumeration. By default, these constants are integers, starting from 0 for the first constant and incrementing by 1 for each subsequent constant.

Here's an example of how to define and use an enumeration in C:

c Copy Code
#include <stdio.h>

// Define an enumeration for days of the week
enum Days {
    SUNDAY,
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY
};

int main() {
    enum Days today = WEDNESDAY;

    // You can use enum constants like regular variables
    printf("Today is %d\n", today);

    return 0;
}

In this example, the 'enum Days' defines an enumeration for the days of the week. Each day is represented by an enumerator, such as 'SUNDAY', 'MONDAY', etc. The value of 'today' is set to 'WEDNESDAY', and you can use the 'enum' constants just like regular integers.

Output:
Today is 3

* Enums can also be explicitly assigned values if you want to specify custom integer values for the constants.

Assign Values:

You can assign specific integer values to each constant in the enumeration. For example:

enum Months {
    RED = 1,
    GREEN = 2,
    BLUE = 3,
    /* ... */
};
c Copy Code
#include <stdio.h>

enum Color
{
    RED = 1,
    GREEN = 2,
    BLUE = 4,
    YELLOW = 8
};

int main()
{
    enum Color myColor = GREEN;
    switch (myColor)
    {
    case RED:
        printf("The color is Red\n");
        break;
    case GREEN:
        printf("The color is Green\n");
        break;
    case BLUE:
        printf("The color is Blue\n");
        break;
    case YELLOW:
        printf("The color is Yellow\n");
        break;
    default:
        printf("Unknown color\n");
    }
    return 0;
}
Output:
The color is Green

In this example, we have defined an 'enum' called 'Color' with explicit values assigned to each constant. You can then declare variables of this 'enum' type and assign one of its values. The values assigned are integers, and you can use these constants in your code as shown in the 'switch' statement.

Structure & Enumeration

Let's combine 'enum' and 'struct' and create a C program. In this program, we'll define a structure to store employee details, and we'll use an enumeration to represent the department each employee belongs to.

c Copy Code
#include <stdio.h>
#include <string.h>

// Define an enumeration for departments
enum Department {
    HR,
    IT,
    SALES,
    MARKETING,
    FINANCE
};

// Define a structure to represent an employee
struct Employee {
    char name[50];
    int employeeID;
    enum Department department;
    double salary;
};

int main() {
    // Create an array of employees
    struct Employee employees[3];

    // Initialize employee information
    employees[0].employeeID = 101;
    strcpy(employees[0].name, "Sanvi");
    employees[0].department = HR;
    employees[0].salary = 50000.0;

    employees[1].employeeID = 102;
    strcpy(employees[1].name, "Ayan");
    employees[1].department = IT;
    employees[1].salary = 60000.0;

    employees[2].employeeID = 103;
    strcpy(employees[2].name, "Suman");
    employees[2].department = SALES;
    employees[2].salary = 55000.0;

    // Display employee information
    printf("Employee Information:\n");
    for (int i = 0; i < 3; i++) {
        printf("Employee ID: %d\n", employees[i].employeeID);
        printf("Name: %s\n", employees[i].name);
        printf("Department: ");
        
        // Use a switch statement to print the department name
        switch (employees[i].department) {
            case HR:
                printf("HR\n");
                break;
            case IT:
                printf("IT\n");
                break;
            case SALES:
                printf("Sales\n");
                break;
            default:
                printf("Unknown\n");
                break;
        }
        
        printf("Salary: %.2f\n\n", employees[i].salary);
    }

    return 0;
}
Output:
Employee Information:
Employee ID: 101     
Name: Sanvi
Department: HR       
Salary: 50000.00

Employee ID: 102
Name: Ayan
Department: IT
Salary: 60000.00

Employee ID: 103
Name: Suman
Department: Sales
Salary: 55000.00

What's Next?

We've now entered the finance section on this platform, where you can enhance your financial literacy.