【发布时间】:2010-01-29 12:15:54
【问题描述】:
是否有一种单行方法可以让我输出枚举的当前值?
【问题讨论】:
是否有一种单行方法可以让我输出枚举的当前值?
【问题讨论】:
作为一个字符串,不。作为整数,%d。
除非你计算:
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]);
这不适用于位掩码枚举。此时,您需要一个哈希表或其他更复杂的数据结构。
【讨论】:
enum MyEnum
{ A_ENUM_VALUE=0,
B_ENUM_VALUE,
C_ENUM_VALUE
};
int main()
{
printf("My enum Value : %d\n", (int)C_ENUM_VALUE);
return 0;
}
你只需要将 enum 转换为 int !
输出:我的枚举值:2
【讨论】:
enum MyEnum c = C_ENUM_VALUE; 然后通过c,他将需要演员表。请参阅下面关于@Neil 的回答的讨论。
已经给出了正确答案:不,你不能给出枚举的名称,只能给出它的值。
尽管如此,只是为了好玩,这将为您提供一个枚举和一个查找表,并为您提供一种按名称打印它的方法:
main.c:
#include "Enum.h"
CreateEnum(
EnumerationName,
ENUMValue1,
ENUMValue2,
ENUMValue3);
int main(void)
{
int i;
EnumerationName EnumInstance = ENUMValue1;
/* Prints "ENUMValue1" */
PrintEnumValue(EnumerationName, EnumInstance);
/* Prints:
* ENUMValue1
* ENUMValue2
* ENUMValue3
*/
for (i=0;i<3;i++)
{
PrintEnumValue(EnumerationName, i);
}
return 0;
}
枚举.h:
#include <stdio.h>
#include <string.h>
#ifdef NDEBUG
#define CreateEnum(name,...) \
typedef enum \
{ \
__VA_ARGS__ \
} name;
#define PrintEnumValue(name,value)
#else
#define CreateEnum(name,...) \
typedef enum \
{ \
__VA_ARGS__ \
} name; \
const char Lookup##name[] = \
#__VA_ARGS__;
#define PrintEnumValue(name, value) print_enum_value(Lookup##name, value)
void print_enum_value(const char *lookup, int value);
#endif
枚举.c
#include "Enum.h"
#ifndef NDEBUG
void print_enum_value(const char *lookup, int value)
{
char *lookup_copy;
int lookup_length;
char *pch;
lookup_length = strlen(lookup);
lookup_copy = malloc((1+lookup_length)*sizeof(char));
strcpy(lookup_copy, lookup);
pch = strtok(lookup_copy," ,");
while (pch != NULL)
{
if (value == 0)
{
printf("%s\n",pch);
break;
}
else
{
pch = strtok(NULL, " ,.-");
value--;
}
}
free(lookup_copy);
}
#endif
免责声明:不要这样做。
【讨论】:
enum A { foo, bar } a;
a = foo;
printf( "%d", a ); // see comments below
【讨论】:
int。 (例如,枚举变量可能与long 兼容,就可以了)。 printf 的%d 但是想要完全 类型int,所以我认为最好进行演员阵容。只有枚举数(声明列表中的那个)的类型正好是int。
C_ENUM_VALUE 被称为“枚举器”,在 C 中它总是输入 int。但是a是一个枚举变量,它具有枚举的类型。但是在 C 中,类型可以兼容(这意味着如果 U 和 T 兼容,则可以使用 T *t = &some_u 等)。许多规则仅根据兼容性而不是类型相等来说明。枚举类型与char 或有符号或无符号整数类型兼容。你不知道,最好不要对它做任何假设。
有些家伙在这篇文章中提出了一个智能预处理器的想法
【讨论】:
我遇到了同样的问题。
我必须打印颜色所在的节点的颜色:enum col { WHITE, GRAY, BLACK }; 和节点:typedef struct Node { col color; };
我尝试用printf("%s\n", node->color); 打印node->color,但我在屏幕上看到的只是(null)\n。
bmargulies给出的答案几乎解决了这个问题。
所以我的最终解决方案是:
static char *enumStrings[] = {"WHITE", "GRAY", "BLACK"};
printf("%s\n", enumStrings[node->color]);
【讨论】:
printf("%s\n", enumStrings[node->color]); 因为您现在正在访问数组本身以查找相应的字符串。
打印枚举值可能会很棘手,因为每个成员的大小可能因实现而异。以这个在 gcc 8.4.0 上编译的例子为例。
#include <stdio.h>
int main(void) {
enum option {A = 0, B = 0x100000000};
// Enumerator sizes of the same enumeration can differ
printf("sizeof(A)=%zu\n", sizeof(A)); // sizeof(A)=4
printf("sizeof(B)=%zu\n", sizeof(B)); // sizeof(B)=8
// Same output even though they have different values
printf("A=%d\n", A); // A=0
printf("B=%d\n", B); // B=0
// You should know beforehand the maximum enumerator size
printf("A=%ld\n", A); // A=0
printf("B=%ld\n", B); // B=4294967296
}
【讨论】: