我可以用printf()显示枚举的值吗? [英] Can I display the value of an enum with printf()?

查看:337
本文介绍了我可以用printf()显示枚举的值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有单行代码可以让我输出枚举的当前值?

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

推荐答案

作为字符串,否。

除非您计数:

static char* enumStrings[] = { /* filler 0's to get to the first value, */
                               "enum0", "enum1", 
                               /* filler for hole in the middle: ,0 */
                               "enum2", "enum3", .... };

...

printf("The value is %s\n", enumStrings[thevalue]);

这不适用于枚举位掩码之类的东西。到那时,您需要一个哈希表或其他更精细的数据结构。

This won't work for something like an enum of bit masks. At that point, you need a hash table or some other more elaborate data structure.

这篇关于我可以用printf()显示枚举的值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆