C Enumerated Types

By

Learn how to create enumerated types in C with the typedef and enum keywords, defining a type that can hold one of a fixed set of values like weekdays.

~~~

An enumerated type in C is a type whose value must be one of a fixed set of named constants. Using the typedef and enum keywords we can define a type that can have either one value or another.

It’s one of the most important uses of the typedef keyword.

This is the syntax of an enumerated type:

typedef enum {
  //...values
} TYPENAME;

The enumerated type we create is usually, by convention, uppercase.

Here is a simple example:

typedef enum {
  true,
  false
} BOOLEAN;

C comes with a bool type, so this example is not really practical, but you get the idea.

Another example is to define weekdays:

typedef enum {
  monday,
  tuesday,
  wednesday,
  thursday,
  friday,
  saturday,
  sunday
} WEEKDAY;

Here’s a simple program that uses this enumerated type:

#include <stdio.h>

typedef enum {
  monday,
  tuesday,
  wednesday,
  thursday,
  friday,
  saturday,
  sunday
} WEEKDAY;

int main(void) {
  WEEKDAY day = monday;

  if (day == monday) {
    printf("It's monday!");
  } else {
    printf("It's not monday");
  }
}

The values behind the names

Every item in the enum definition is paired to an integer, internally. So in this example monday is 0, tuesday is 1 and so on.

This means the conditional could have been if (day == 0) instead of if (day == monday), but it’s way simpler for us humans to reason with names rather than numbers, so it’s a very convenient syntax.

You can also choose the values yourself. Assign a value to one constant and the ones after it continue counting from there:

typedef enum {
  monday = 1,
  tuesday,
  wednesday
} WEEKDAY;

Here monday is 1, tuesday is 2, wednesday is 3. This is handy when 0 has a special meaning in your program, like “no day selected”.

Using an enum with switch

Enums pair naturally with switch, because you’re checking one value against a fixed set of options:

switch (day) {
  case saturday:
  case sunday:
    printf("It's the weekend!");
    break;
  default:
    printf("Back to work");
}

The compiler can even warn you when a switch on an enum forgets one of the possible values, which is a nice safety net.

Be careful with the ordering

Look back at the BOOLEAN example. Since true is listed first, true is 0 and false is 1.

That’s the opposite of how C evaluates conditions, where 0 means false. So if (day == true) works, but writing if (value) with a BOOLEAN set to true would fail, because true is 0.

The fix is to list false first, so it gets the value 0. Or better, use the built-in bool type and reserve enums for cases like weekdays, where the set of values is truly yours.

One more thing to know: an enum variable is just an integer under the hood. The compiler won’t stop you from assigning day = 42, even though 42 doesn’t match any weekday. Stick to the named constants and you’ll be fine.

Tagged: C · All topics
~~~

Related posts about clang: