Enum with Switch in C
Level 5317 keystrokes
Defines an enum of four states and a function that uses switch to print a readable name for each one. Calling it with IDLE, RUNNING and STOPPED prints Idle, Running and Stopped.
C code
// Enumerators count up from 0: IDLE=0, RUNNING=1, PAUSED=2, STOPPED=31enum State { IDLE, RUNNING, PAUSED, STOPPED };2 // Prints a readable name for a state3void printState(enum State s) { // Jumps to the case label that matches s4 switch (s) { // One case per state; break stops execution from falling into the next case5 case IDLE: printf("Idle\n"); break;6 case RUNNING: printf("Running\n"); break;7 case PAUSED: printf("Paused\n"); break;8 case STOPPED: printf("Stopped\n"); break;9 }10}11 // Prints Idle12printState(IDLE);// Prints Running13printState(RUNNING);// Prints Stopped; PAUSED is never used here14printState(STOPPED);Output
Idle Running Stopped
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.
;× 12(× 9)× 9"× 8:× 4\× 4,× 3{× 3}× 3