Map enum to char and output with for loop : Enum « Data Type « C / ANSI-C






Map enum to char and output with for loop



#include <stdio.h>

enum spectrum { red, orange, yellow} colour;

char *rainbow[] = { "red", "orange", "yellow" };

int main() {
  
  for ( colour = red; colour <= yellow; colour++ ) {
      printf ( "%s ", rainbow[colour]);
  }

  printf ( "\n" );

}

           
       








Related examples in the same category

1.Map enum to char array
2.Define and use enum
3.Reference enum value by int
4.Map enum to string array