Easy way to use variables of enum types as string in C?

前端 未结 19 2332
太阳男子
太阳男子 2020-11-22 08:47

Here\'s what I am trying to do:

typedef enum { ONE, TWO, THREE } Numbers;

I am trying to write a function that would do a switch case sim

19条回答
  •  故里飘歌
    2020-11-22 09:00

    #define stringify( name ) # name
    
    enum MyEnum {
        ENUMVAL1
    };
    ...stuff...
    
    stringify(EnumName::ENUMVAL1);  // Returns MyEnum::ENUMVAL1
    

    Further discussion on this method

    Preprocessor directive tricks for newcomers

提交回复
热议问题