我需要将枚举成员(其值,而不是标识符)转换为字符串。 我已经尝试了以下,它适用于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来测试结果...
请详细说明您的奇怪要求。如果你需要字符串,你可以使用sprintf()而不是printf()。 – qrdl 2009-10-03 18:55:43