【问题标题】:Can I display the value of an enum with printf()?我可以用 printf() 显示枚举的值吗?
【发布时间】:2010-01-29 12:15:54
【问题描述】:

是否有一种单行方法可以让我输出枚举的当前值?

【问题讨论】:

    标签: c printf enums


    【解决方案1】:

    作为一个字符串,不。作为整数,%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]);
    

    这不适用于位掩码枚举。此时,您需要一个哈希表或其他更复杂的数据结构。

    【讨论】:

    • 这(当然)意味着你的枚举真的从 0 开始,并且是连续的,没有“洞”。
    【解决方案2】:
    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

    【讨论】:

    • @aib,不过,很容易忽略什么时候施放什么时候施放。 “枚举”是什么意思?我会一直做演员。上面的演员表是多余的,因为它使用了枚举器。但如果他完成了enum MyEnum c = C_ENUM_VALUE; 然后通过c,他将需要演员表。请参阅下面关于@Neil 的回答的讨论。
    • @Johannes,据我所知,您是对的。感谢您的明确解释!
    【解决方案3】:

    已经给出了正确答案:不,你不能给出枚举的名称,只能给出它的值。

    尽管如此,只是为了好玩,这将为您提供一个枚举和一个查找表,并为您提供一种按名称打印它的方法:

    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
    

    免责声明:不要这样做。

    【讨论】:

      【解决方案4】:
      enum A { foo, bar } a;
      a = foo;
      printf( "%d", a );   // see comments below
      

      【讨论】:

      • 真的需要强制转换为 int 吗?在 C 中,枚举总是 int 类型。
      • 可能不会 - 我会改变它。
      • @Maurits,@Neil,需要演员阵容。枚举变量是一种不同的类型,并且与 some 整数类型兼容,能够存储枚举数的所有值。这不一定是int。 (例如,枚举变量可能与long 兼容,就可以了)。 printf 的%d 但是想要完全 类型int,所以我认为最好进行演员阵容。只有枚举数(声明列表中的那个)的类型正好是int
      • @Johannes 不是对可变参数执行了一些标准转换吗?
      • @Maurits,是的。 C_ENUM_VALUE 被称为“枚举器”,在 C 中它总是输入 int。但是a是一个枚举变量,它具有枚举的类型。但是在 C 中,类型可以兼容(这意味着如果 UT 兼容,则可以使用 T *t = &amp;some_u 等)。许多规则仅根据兼容性而不是类型相等来说明。枚举类型与char 或有符号或无符号整数类型兼容。你不知道,最好不要对它做任何假设。
      【解决方案5】:

      有些家伙在这篇文章中提出了一个智能预处理器的想法

      Easy way to use variables of enum types as string in C?

      【讨论】:

        【解决方案6】:

        我遇到了同样的问题。

        我必须打印颜色所在的节点的颜色:enum col { WHITE, GRAY, BLACK }; 和节点:typedef struct Node { col color; };

        我尝试用printf("%s\n", node-&gt;color); 打印node-&gt;color,但我在屏幕上看到的只是(null)\n

        bmargulies给出的答案几乎解决了这个问题。

        所以我的最终解决方案是:

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

        【讨论】:

        • 我相信这实际上应该修改为:printf("%s\n", enumStrings[node-&gt;color]); 因为您现在正在访问数组本身以查找相应的字符串。
        【解决方案7】:

        打印枚举值可能会很棘手,因为每个成员的大小可能因实现而异。以这个在 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
        }
        
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-10-02
          • 2015-03-02
          • 2010-09-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多