Skip to main content

C Enumerations

Enumerations, often referred to as "enums," are user-defined data types in C that consist of integral constants. Enums make your code more readable, maintainable, and self-documenting by allowing you to define a set of named constants.

What are Enumerations?

An enumeration is a user-defined data type that consists of integral constants, where each constant is given a name. By default, the first enumeration constant has the value 0, and each subsequent constant is incremented by 1.

c
enum boolean {
FALSE, // value is 0
TRUE // value is 1
};

Declaring an Enumeration

The general syntax for declaring an enumeration is:

c
enum tag_name {
enumerator1,
enumerator2,
/* ... */
enumeratorN
};

Here's an example:

c
enum days {
SUNDAY,
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY
};

Using Enumerations

You can use enumerations to create variables of the enum type:

c
enum days today;
today = MONDAY;

if (today == MONDAY) {
printf("It's Monday, back to work!\n");
}

Assigning Custom Values

By default, enum constants are assigned increasing integer values starting from 0. However, you can assign custom values to enum constants:

c
enum months {
JAN = 1, // starts from 1 instead of 0
FEB, // automatically becomes 2
MAR, // automatically becomes 3
APR = 10, // explicitly set to 10
MAY, // automatically becomes 11
JUN // automatically becomes 12
};

Enum Variables and Memory

Enum variables typically occupy the same amount of memory as an int in most C implementations (usually 4 bytes), regardless of the range of values in the enumeration.

c
enum direction {NORTH, EAST, SOUTH, WEST};
printf("Size of enum direction: %zu bytes\n", sizeof(enum direction)); // Usually prints 4

Declaring Enum Variables

You can declare enum variables in three ways:

c
// Method 1: Declare an enum type, then declare variables of that type
enum colors {RED, GREEN, BLUE};
enum colors favorite_color;

// Method 2: Declare the enum and variables together
enum fruits {APPLE, BANANA, ORANGE} fruit1, fruit2;

// Method 3: Anonymous enum (no tag name)
enum {SMALL, MEDIUM, LARGE} size;

Enumerations vs. #define

Enumerations provide several advantages over using #define directives for constants:

  1. Enum values are automatically assigned if not explicitly specified
  2. Enums have scope and type checking
  3. Enums can be used with a debugger more effectively
  4. Enums can be forward-declared
c
// Using #define
#define RED 0
#define GREEN 1
#define BLUE 2

// Using enum (preferred)
enum colors {RED, GREEN, BLUE};

Example: Using Enums in a Switch Statement

Enums are particularly useful with switch statements, making code more readable:

c
#include <stdio.h>

enum Weekday {MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY};

int main() {
enum Weekday today = WEDNESDAY;

switch (today) {
case MONDAY:
printf("It's Monday!\n");
break;
case TUESDAY:
printf("It's Tuesday!\n");
break;
case WEDNESDAY:
printf("It's Wednesday!\n");
break;
case THURSDAY:
printf("It's Thursday!\n");
break;
case FRIDAY:
printf("It's Friday!\n");
break;
case SATURDAY:
case SUNDAY:
printf("It's the weekend!\n");
break;
}

return 0;
}

TypeDef with Enum

You can use typedef with enums to simplify the declaration of enum variables:

c
typedef enum {
CIRCLE,
RECTANGLE,
TRIANGLE
} Shape;

// Now you can use 'Shape' directly instead of 'enum Shape'
Shape myShape = RECTANGLE;

Best Practices

  1. Use ALL_CAPS for enum constants to distinguish them from variables
  2. Provide meaningful names for enum types and constants
  3. Use enums instead of magic numbers to improve code readability
  4. Consider using typedefs with enums to simplify variable declarations
  5. Be aware that enums are integers and can be used in arithmetic operations (but use with caution)

Conclusion

Enumerations provide a clean and maintainable way to define named constants in C. They enhance code readability, provide type safety, and make debugging easier. By using enums appropriately, you can write more self-documenting code that's easier to maintain and understand.



If you spot any mistakes on this website, please let me know at [email protected]. I’d greatly appreciate your feedback! :)