枚举成员的字符串化

bot*_*ius 2 c macros

我需要将枚举成员(其值,而不是标识符)转换为字符串.我尝试了以下,它适用于MACRO(TYPE_A),但不适用于枚举值(typeA).在我看来,这有点奇怪.

你知道怎么做吗?


#define _tostr(a) #a
#define tostr(a) _tostr(a)

typedef enum _SPECIAL_FOLDER_ID {
    typeA = 3,
    typeB = 4,
} SPECIAL_FOLDER_ID;

#define TYPE_A 3

int main() {
    //this is working, but the information is a macro (TYPE_A)
    printf("The string is " tostr(TYPE_A) ".\n");

    //this is not working for typeA (defined in an enumeration)
    printf("The string is " tostr(typeA) ".\n");
    return 0;
}


输出是:

The string is 3.
The string is typeA.

我需要以某种方式修改代码,以便输出的第二行是"字符串是3".

谢谢!

PS:我不想使用printf打印该值.我需要一个包含该值的静态字符串.我只使用printf来测试结果......

pmg*_*pmg 6

预处理器不知道C.它只知道"文本".
处理文件时,typeA只需5个字母.只有编译器才会知道(在预处理器完成之后)typeA有一个值,并且值为3.