将枚举与C中的字符串相关联

use*_*944 5 c string enums

我看过这个链接

如何在c中将枚举名称转换为字符串

enums在客户端提供的库头文件(我无法更改)中以下列方式定义了一系列:

枚举也很稀疏.

typedef enum
{
    ERROR_NONE=59,   
    ERROR_A=65,  
    ERROR_B=67
}
Run Code Online (Sandbox Code Playgroud)

我想在我的函数中打印这些值,例如我想打印ERROR_NONE而不是59.有没有更好的方法来使用switch caseif else构造来完成这项工作?例

   int Status=0;
   /* some processing in library where Status changes to 59 */
   printf("Status = %d\n",Status); /* want to print ERROR_NONE instead of 59 */
Run Code Online (Sandbox Code Playgroud)

dir*_*tly 2

常见问题 11.17。使用xstr()宏。你可能应该使用它:

 #define str(x) #x
 #define xstr(x) str(x)

 printf("%s\n", xstr(ERROR_A));
Run Code Online (Sandbox Code Playgroud)