Enum in C

Enum in C. Enum in C Tutorial. Learn Enum in C
Enum in C

What is Enum in C ?

An enum (short for enumeration) in C is a user-defined data type that consists of a set of named integer constants. It is used to create a symbolic representation for a set of related integer values, making code more readable and maintainable.

Syntax of Enum:

enum enum_name {
    enumeration_constant1,
    enumeration_constant2,
    // ... more constants
};

Where,

  1. enum_name: This is where you specify the name of your enumeration type. It should be a valid C identifier and is used to refer to the enum type when declaring variables of that type.
  2. enumeration_constant1, enumeration_constant2, …: This is where you list the individual constants (also called enumerators) that belong to your enum. These constants are separated by commas and represent the possible values the enum can take. Each constant is implicitly assigned an integer value starting from 0 for the first constant, and the subsequent constants are assigned values one greater than the previous one. You can also explicitly assign values to these constants if needed.

Example:

enum Color {
    Red,       // 0
    Green,     // 1
    Blue       // 2
};

In above example, “enum_name” is “Color,” and the constants “Red,” “Green,” and “Blue” are listed within the curly braces.

Examples of Enum in C

Example 1: Simple Enum

#include <stdio.h>

enum Weekdays {
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
    Sunday
};

int main() {
    enum Weekdays today = Wednesday;
    printf("Today is %d\n", today);
    return 0;
}

Output:

Today is 2

Explanation:

In above example, we define an enum Weekdays with seven constants. Wednesday is assigned the value 2 by default. We then declare a variable today and assign it the value Wednesday, which is printed as 2.

Example 2: Enum with Explicit Values

#include <stdio.h>

enum Months {
    January = 1,
    February = 2,
    March = 3,
    April = 4,
    May = 5,
    June = 6,
    July = 7,
    August = 8,
    September = 9,
    October = 10,
    November = 11,
    December = 12
};

int main() {
    enum Months currentMonth = October;
    printf("The current month is %d\n", currentMonth);
    return 0;
}

Output:

The current month is 10

Explanation:

In above example, we define an enum Months with explicit values for each month. October is assigned the value 10. We then declare a variable currentMonth and assign it the value October, which is printed as 10.

Example 3: Enum with Bit Flags

#include <stdio.h>

enum Permissions {
    Read = 1,      // 0001
    Write = 2,     // 0010
    Execute = 4,   // 0100
    Admin = 8      // 1000
};

int main() {
    enum Permissions user = Read | Write;
    printf("User permissions: %d\n", user);

    if (user & Admin) {
        printf("User has admin rights.\n");
    }

    return 0;
}

Output:

User permissions: 3

Explanation:

In above example, we use an enum Permissions to represent permission flags using bit-wise OR (|) to combine them. user is assigned the value Read | Write, which is 3 (binary 0011). We demonstrate checking admin rights using bit-wise AND (&) with the Admin flag.

Example 4: Enum with Custom Values and Operations

#include <stdio.h>

enum Color {
    Red = 10,
    Green = 20,
    Blue = 30
};

int main() {
    enum Color selectedColor = Blue;
    printf("Selected color has a value of %d\n", selectedColor);

    // Increment the color value by 5
    selectedColor += 5;
    printf("After incrementing, color has a value of %d\n", selectedColor);

    return 0;
}

Output:

Selected color has a value of 30
After incrementing, color has a value of 35

Explanation:

In above example, we define an enum Color with custom values. Blue is explicitly assigned the value 30. We then increment the selectedColor by 5, demonstrating that you can perform arithmetic operations on enum values.

Example 5: Enum within a struct

#include <stdio.h>

enum EngineType {
    Petrol,
    Diesel,
    Electric
};

struct Car {
    char brand[20];
    int year;
    enum EngineType engine;
};

int main() {
    struct Car myCar = {"Toyota", 2023, Electric};
    printf("My car is a %d-powered %s from %d\n", myCar.engine, myCar.brand, myCar.year);

    return 0;
}

Output:

My car is a 2-powered Toyota from 2023

Explanation:

In above example, we define an enum EngineType to represent different engine types. We then create a struct called Car that includes an enum variable engine along with other information. This demonstrates how enums can be used as part of larger data structures like structs.

Rules to be followed when using enum in C:

  1. Enumeration constants are integers by default and start from 0 unless specified.
  2. You can explicitly assign values to enumeration constants.
  3. The enumeration constants must have unique names within the same enum.
  4. You can declare variables of the enum type to store its values.
  5. Enums can be used in switch statements for easy branching based on their values.

More: