Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print text instead of value from C enum

Tags:

c

enums

int main() {    enum Days{Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday};    Days TheDay;    int j = 0;    printf("Please enter the day of the week (0 to 6)\n");    scanf("%d",&j);    TheDay = Days(j);    //how to PRINT THE VALUES stored in TheDay    printf("%s",TheDay);  //   isnt working    return 0; } 
like image 926
avi Avatar asked Jul 02 '10 18:07

avi


2 Answers

Enumerations in C are numbers that have convenient names inside your code. They are not strings, and the names assigned to them in the source code are not compiled into your program, and so they are not accessible at runtime.

The only way to get what you want is to write a function yourself that translates the enumeration value into a string. E.g. (assuming here that you move the declaration of enum Days outside of main):

const char* getDayName(enum Days day)  {    switch (day)     {       case Sunday: return "Sunday";       case Monday: return "Monday";       /* etc... */    } }  /* Then, later in main: */ printf("%s", getDayName(TheDay)); 

Alternatively, you could use an array as a map, e.g.

const char* dayNames[] = {"Sunday", "Monday", "Tuesday", /* ... etc ... */ };  /* ... */  printf("%s", dayNames[TheDay]); 

But here you would probably want to assign Sunday = 0 in the enumeration to be safe... I'm not sure if the C standard requires compilers to begin enumerations from 0, although most do (I'm sure someone will comment to confirm or deny this).

like image 145
Tyler McHenry Avatar answered Sep 27 '22 21:09

Tyler McHenry


I use something like this:

in a file "EnumToString.h":

#undef DECL_ENUM_ELEMENT #undef DECL_ENUM_ELEMENT_VAL #undef DECL_ENUM_ELEMENT_STR #undef DECL_ENUM_ELEMENT_VAL_STR #undef BEGIN_ENUM #undef END_ENUM  #ifndef GENERATE_ENUM_STRINGS     #define DECL_ENUM_ELEMENT( element ) element,     #define DECL_ENUM_ELEMENT_VAL( element, value ) element = value,     #define DECL_ENUM_ELEMENT_STR( element, descr ) DECL_ENUM_ELEMENT( element )     #define DECL_ENUM_ELEMENT_VAL_STR( element, value, descr ) DECL_ENUM_ELEMENT_VAL( element, value )     #define BEGIN_ENUM( ENUM_NAME ) typedef enum tag##ENUM_NAME     #define END_ENUM( ENUM_NAME ) ENUM_NAME; \             const char* GetString##ENUM_NAME(enum tag##ENUM_NAME index); #else     #define BEGIN_ENUM( ENUM_NAME) const char * GetString##ENUM_NAME( enum tag##ENUM_NAME index ) {\         switch( index ) {      #define DECL_ENUM_ELEMENT( element ) case element: return #element; break;     #define DECL_ENUM_ELEMENT_VAL( element, value ) DECL_ENUM_ELEMENT( element )     #define DECL_ENUM_ELEMENT_STR( element, descr ) case element: return descr; break;     #define DECL_ENUM_ELEMENT_VAL_STR( element, value, descr ) DECL_ENUM_ELEMENT_STR( element, descr )      #define END_ENUM( ENUM_NAME ) default: return "Unknown value"; } } ;  #endif 

then in any header file you make the enum declaration, day enum.h

#include "EnumToString.h"  BEGIN_ENUM(Days) {     DECL_ENUM_ELEMENT(Sunday) //will render "Sunday"     DECL_ENUM_ELEMENT(Monday) //will render "Monday"     DECL_ENUM_ELEMENT_STR(Tuesday, "Tuesday string") //will render "Tuesday string"     DECL_ENUM_ELEMENT(Wednesday) //will render "Wednesday"     DECL_ENUM_ELEMENT_VAL_STR(Thursday, 500, "Thursday string") // will render "Thursday string" and the enum will have 500 as value     /* ... and so on */ } END_ENUM(MyEnum) 

then in a file called EnumToString.c:

#include "enum.h"  #define GENERATE_ENUM_STRINGS  // Start string generation  #include "enum.h"               #undef GENERATE_ENUM_STRINGS   // Stop string generation 

then in main.c:

int main(int argc, char* argv[]) {     Days TheDay = Monday;     printf( "%d - %s\n", TheDay, GetStringDay(TheDay) ); //will print "1 - Monday"      TheDay = Thursday;     printf( "%d - %s\n", TheDay, GetStringDay(TheDay) ); //will print "500 - Thursday string"      return 0; } 

this will generate "automatically" the strings for any enums declared this way and included in "EnumToString.c"

like image 38
Vargas Avatar answered Sep 27 '22 21:09

Vargas