How does Enum allocate Memory on C?

前端 未结 3 602
渐次进展
渐次进展 2020-12-18 03:58

I\'m trying to work with C and Assembly (intelx8086) lenguage.

I\'m also using one class a friend of mine gave me that has a

typedef enum data_10 {a=         


        
相关标签:
3条回答
  • 2020-12-18 04:46

    Although it may vary from compiler to compiler, enum typically takes the same size as an int. To be sure, though, you can always use sizeof( data_10_type );

    0 讨论(0)
  • 2020-12-18 04:59

    Why don't you print it?

    /* C99 */
    #include <stdio.h>
    
    typedef enum { a = 0, b = 7, c = 10 } data_10_type;
    printf("%zu\n", sizeof(data_10_type));
    

    The identifiers in an enumerator list are declared as constants that have type int (C11 §6.7.2.2 Enumeration specifiers), so sizeof(data_10_type) is often equal to sizeof(int), but it isn't necessary!

    BTW, if you want to have a size in bits, just use the CHAR_BIT constant (defined in <limits.h>), which indicates how many bits there are in a single byte).

    /* C99 */
    #include <limits.h>
    #include <stdio.h>
    
    typedef enum { a = 0, b = 7, c = 10 } data_10_type;
    printf("%zu\n", sizeof(data_10_type) * CHAR_BIT);
    
    0 讨论(0)
  • 2020-12-18 04:59

    An enum does not really take any memory at all; it's understood by the compiler and the right numbers get used during compilation. It's an int, whose size is dependent on your system.

    0 讨论(0)
提交回复
热议问题