Can I display the value of an enum with printf()?

后端 未结 6 836
太阳男子
太阳男子 2020-12-24 05:13

Is there a one-liner that lets me output the current value of an enum?

6条回答
  •  长情又很酷
    2020-12-24 05:50

    I had the same problem.

    I had to print the color of the nodes where the color was: enum col { WHITE, GRAY, BLACK }; and the node: typedef struct Node { col color; };

    I tried to print node->color with printf("%s\n", node->color); but all I got on the screen was (null)\n.

    The answer bmargulies gave almost solved the problem.

    So my final solution is:

    static char *enumStrings[] = {"WHITE", "GRAY", "BLACK"};
    printf("%s\n", enumStrings[node->color]);
    

提交回复
热议问题