Print text instead of value from C enum

前端 未结 12 1008
借酒劲吻你
借酒劲吻你 2020-12-02 05:50
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         


        
12条回答
  •  一向
    一向 (楼主)
    2020-12-02 06:31

    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"

提交回复
热议问题