We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Enums

Unlike Golang (a language living in 1970), C has explicit support for enums (enumerations) with the enum keyword.

TJ is salty because Go is a simple, modern language that companies ackshually use to ship products. Not all programming languages can be academic thought-experiments like OCaml.

You can define a new enum type like this:

typedef enum DaysOfWeek {
  MONDAY,
  TACO_TUESDAY,
  WEDNESDAY,
  THURSDAY,
  FRIDAY,
  SATURDAY,
  FUNDAY,
} days_of_week_t;

The typedef and its alias days_of_week_t are optional, but like with structs, they make the enum easier to use.

In the example above, days_of_week_t is a new type that can only have one of the values defined in the enum:

  • MONDAY, which is 0
  • TACO_TUESDAY, which is 1
  • WEDNESDAY, which is 2
  • THURSDAY, which is 3
  • FRIDAY, which is 4
  • SATURDAY, which is 5
  • FUNDAY, which is 6

You can use the enum type like this:

typedef struct Event {
  char *title;
  days_of_week_t day;
} event_t;

// Or if you don't want to use the alias:

typedef struct Event {
  char *title;
  enum DaysOfWeek day;
} event_t;

An enum is not a collection type like a struct or an array. It's just a list of integers constrained to a new type, where each is given an explicit name.

Assignment

The Sneklang graphics library needs to represent colors.

Create a Color enum (and the color_t typedef) with RED, GREEN, and BLUE values, in that order.