Enum Type in C

Level 4180 keystrokes

Uses enum to define named constants and checks if it's a weekday.

C code

// MON is set to 1; each following name is one more (TUE = 2, WED = 3 ... SUN = 7)
1enum Day { MON = 1, TUE, WED, THU, FRI, SAT, SUN };
// Variable of type enum Day holding WED (3)
2enum Day today = WED;
// Enum values are integers, so they can be compared: 1 to 5 is a weekday
3if (today >= MON && today <= FRI) {
// %d prints the underlying number: Weekday: 3
4 printf("Weekday: %d", today);
5} else {
// SAT (6) and SUN (7) would end up here
6 printf("Weekend: %d", today);
7}

Output

Weekday: 3
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this C code and how often they appear.

  • ,× 8
  • ;× 4
  • "× 4
  • =× 4
  • (× 3
  • )× 3
  • {× 3
  • }× 3
  • :× 2
  • &× 2

More C examples at level 4

← C typing practice