如何将枚举类型变量转换为字符串?

新手上路,请多包涵

如何使 printf 显示枚举类型的变量的值?例如:

 typedef enum {Linux, Apple, Windows} OS_type;
OS_type myOS = Linux;

我需要的是类似

printenum(OS_type, "My OS is %s", myOS);

必须显示字符串“Linux”,而不是整数。

我想,首先我必须创建一个值索引的字符串数组。但我不知道这是否是最美丽的方式。有可能吗?

原文由 psihodelia 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.1k
2 个回答

确实没有漂亮的方法可以做到这一点。只需设置一个由枚举索引的字符串数组。

如果你做了很多输出,你可以定义一个运算符<<,它接受一个枚举参数并为你进行查找。

原文由 Bo Persson 发布,翻译遵循 CC BY-SA 3.0 许可协议

扩展@Reno 的答案,这是一个工作示例,

 #include <stdio.h>

//debug macro, keep it defined or undefine
#define DEBUG
//#undef DEBUG

#ifndef DEBUG
        #define DECL_ENUM_ELEMENT( element ) element
        #define BEGIN_ENUM( ENUM_NAME ) typedef enum
        #define END_ENUM( ENUM_NAME )  ENUM_NAME;
#else
        #define DECL_ENUM_ELEMENT( element ) #element
        #define BEGIN_ENUM( ENUM_NAME ) const char* gs_##ENUM_NAME [] =
        #define END_ENUM( ENUM_NAME ); \
                                int gs_##ENUM_NAME##size = sizeof(gs_##ENUM_NAME)/sizeof(gs_##ENUM_NAME[0]); \
                                const char* MatchEnumToString##ENUM_NAME(int  index) { \
                                if (index > (gs_##ENUM_NAME##size - 1) || index < 0) \
                                { \
                                       return "ERR: invalid"; \
                                }  \
                                else \
                                       return gs_##ENUM_NAME [index]; \
                                }
#endif

BEGIN_ENUM(Days)
{
    DECL_ENUM_ELEMENT(sunday),
    DECL_ENUM_ELEMENT(monday),
    DECL_ENUM_ELEMENT(tuesday),
    DECL_ENUM_ELEMENT(wednesday),
    DECL_ENUM_ELEMENT(thursday),
    DECL_ENUM_ELEMENT(friday),
    DECL_ENUM_ELEMENT(saturday)
}
END_ENUM(Days)

BEGIN_ENUM(fruit)
{
    DECL_ENUM_ELEMENT(apple),
    DECL_ENUM_ELEMENT(orange),
    DECL_ENUM_ELEMENT(mango)
}
END_ENUM(fruit)

void match_etos( int index )
{
#ifdef DEBUG
            printf("Day is %s ,", MatchEnumToStringDays(index) );
            printf("Fruit is %s\n", MatchEnumToStringfruit(index) );
#else
            printf("disabled match_etos, index: %d\n", index);
#endif
}

int main()
{
        match_etos(0);
        match_etos(1);
        match_etos(2);
        match_etos(3);
        match_etos(4);
        match_etos(5);
        match_etos(6);
        match_etos(-43);
#ifdef DEBUG
        printf("gs_Dayssize %d, gs_fruitsize %d\n", gs_Dayssize, gs_fruitsize);
#endif
        return 0;
}

编译上面的例子

g++ <savedfilename>.cpp
./a.out

定义 DEBUG 时的输出,

 Day is sunday ,Fruit is apple
Day is monday ,Fruit is orange
Day is tuesday ,Fruit is mango
Day is wednesday ,Fruit is ERR: invalid
Day is thursday ,Fruit is ERR: invalid
Day is friday ,Fruit is ERR: invalid
Day is saturday ,Fruit is ERR: invalid
Day is ERR: invalid ,Fruit is ERR: invalid
gs_Dayssize 7, gs_fruitsize 3

未定义 DEBUG 时,

 disabled match_etos, index: 0
disabled match_etos, index: 1
disabled match_etos, index: 2
disabled match_etos, index: 3
disabled match_etos, index: 4
disabled match_etos, index: 5
disabled match_etos, index: 6
disabled match_etos, index: -43

如果定义了 DEBUG 宏,它将创建一个数组,如果未定义其原始枚举将在预处理后创建。

原文由 mrigendra 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题