Tuesday, January 14, 2025
HomeTechWhat is Enumeration (or enum) in C

What is Enumeration (or enum) in C

An enumeration in C, abbreviated as enum, is a user-defined data type that allows a programmer to assign names to a set of integral constants. This improves code readability and maintainability by replacing numeric constants with meaningful names.

Syntax of enum

enum enum_name {
    constant1,
    constant2,
    constant3,
    ...
};
  • enum_name: The name of the enumeration.
  • constant1, constant2, ...: The enumeration constants (identifiers).

How enum Works

  • Each constant in the enum is assigned an integer value automatically, starting from 0 by default.
  • The values can also be explicitly specified.
  • Once defined, the constants can be used in the program to represent the respective values.

Example of enum

1. Basic Example

#include <stdio.h>

enum Day {
    SUNDAY,    // 0
    MONDAY,    // 1
    TUESDAY,   // 2
    WEDNESDAY, // 3
    THURSDAY,  // 4
    FRIDAY,    // 5
    SATURDAY   // 6
};

int main() {
    enum Day today;
    today = WEDNESDAY;

    printf("Day number: %d\n", today); // Output: 3
    return 0;
}

2. Enum with Explicit Values

#include <stdio.h>

enum Status {
    SUCCESS = 1,
    FAILURE = -1,
    PENDING = 0
};

int main() {
    enum Status taskStatus;
    taskStatus = SUCCESS;

    if (taskStatus == SUCCESS) {
        printf("Task completed successfully!\n");
    }
    return 0;
}

Properties of enum

  1. Default Values: The constants start from 0 by default, incrementing by 1 for each subsequent constant.
  2. Explicit Assignment: You can assign specific integer values to constants.
  3. Reusability: The same constant names can’t be reused in other enum definitions within the same scope.
See also  Read a file line by line in Python

Applications of enum

  1. Improved Readability: Use named constants instead of integers for better code understanding.
    • Example: MONDAY is more descriptive than 1 when referring to days.
  2. State Management: Useful for managing states in a program, such as task status, error codes, etc.
  3. Code Maintenance: Easy to add, remove, or modify constants without affecting other parts of the program.
See also  Python String Replace: Replace \ with /

Points to Note

  1. Type Compatibility: The enum type is essentially an integer, so enum variables can interact with integer values, but this should be done cautiously.
  2. Scope: The constants in an enum are global within the scope of the enum.
  3. Size: Enum constants are typically of int type unless otherwise specified.

Advanced Example

Enum with Multiple Constants Sharing the Same Value

#include <stdio.h>

enum Color {
    RED = 1,
    GREEN = 2,
    BLUE = 3,
    DEFAULT = 1 // Same as RED
};

int main() {
    printf("RED: %d\n", RED);       // Output: 1
    printf("DEFAULT: %d\n", DEFAULT); // Output: 1
    return 0;
}

Enum with Typedef

#include <stdio.h>

typedef enum {
    SMALL = 1,
    MEDIUM,
    LARGE
} Size;

int main() {
    Size shirtSize = MEDIUM;
    printf("Shirt size: %d\n", shirtSize); // Output: 2
    return 0;
}

Limitations of enum

  1. No Type Safety: Enum constants are treated as integers, which may lead to unintended operations.
  2. Fixed Size: Enum constants are of type int, which might be wasteful in terms of memory for large enumerations.
See also  What is Format Specifiers in C

Conclusion

enum is a powerful feature in C for defining a collection of named integer constants. It enhances code clarity and reduces the chance of errors compared to using raw numeric values directly.

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x