Simvik

The next traffic light colour

Step through the states of an enum. An easy embedded C problem (Structs, enums and typedef), run and graded in the browser.

easy · Structs, enums and typedef · enums · switch

A traffic light goes red, then green, then yellow, then back to red.

Return the state that follows now.

Starter code

typedef enum {
  LIGHT_RED,
  LIGHT_GREEN,
  LIGHT_YELLOW,
} light_t;

light_t next_light(light_t now) {
  return now;
}

Example tests

Red turns green

printf("%d\n", next_light(LIGHT_RED) == LIGHT_GREEN);

Expected output:

1

Green turns yellow

printf("%d\n", next_light(LIGHT_GREEN) == LIGHT_YELLOW);

Expected output:

1

Learn it first

  • Structs, enums and typedef · Grouping values into structs, the dot and the arrow, naming choices with enum, and shorter names with typedef.

More Structs, enums and typedef problems