Mega Code Archive

 
Categories / C / Code Snippets
 

Reference enum value by int

#include <stdio.h> #include <stdlib.h> #include <conio.h> enum animal {cat, dog, lion, tiger} j; int main(void) { printf("Press a key to select animal: "); /* generate a new random number */ while(!kbhit()) rand(); getch(); /* read and discard character */ j = rand() % 4; switch(j) { case cat: printf("cat"); break; case dog: printf("dog"); break; case lion: printf("lion"); break; case tiger: printf("tiger"); } return 0; }