1

I know 'enum' data type is a user defined data type, and the 'enum' variables are the size of 'int'.

  1. The above 'identifier' is a set of constant values, which have a alias name for the constant values, how are these values stored in the memory? i mean how is 'value1' stored i.e '0', and 'value2' stored i.e '20', and 'value3' stored i.e '7000', and 'value4' stored i.e '1234567' in the memory.
  2. I know that the 'enum_variable' has a size of 32 bits, not mandatory. But the confusion part here is that how can 32 bits [if] collectively store all the 'enum' values?

    enum identifier
    {
      value1 = 0, value2 = 20, value3 = 7000, value4 = 1234567
    } enum_variable;
    
    printf("%d\n",sizeof(enum_variable));
    
T A
  • 1,677
  • 4
  • 21
  • 29

2 Answers2

11

Enumeration "values" aren't stored at all, as they are compile-time named constants.

The compiler simply exchanges the use of an enumeration symbol, with the value of it.

Also, the type of an enumeration value is of type int, so the exact size can differ. But because of that, the enumeration values can be any value in the same range as an int.

For more information see e.g. this enumeration reference.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • why does it yield ' 4bytes ' ? what does '4bytes' mean in this context? – shaik nisar ahmed Aug 21 '18 at 09:17
  • 1
    because on your machine integer type is stored in 4 bytes – Bartek Woźniak Aug 21 '18 at 09:18
  • is it a constant of size 4 bytes or a variable of size 4 bytes? – shaik nisar ahmed Aug 21 '18 at 09:24
  • 1
    @shaiknisarahmed On a modern typical 32 or 64 bit machine, the size of `int` is four bytes. On smaller embedded systems it might still be two bytes. And nothing stops it from being eight or more on future systems. – Some programmer dude Aug 21 '18 at 09:25
  • @shaiknisarahmed And as stated in my answer, an enumeration value is a *constant* of type `int`. It's a much nicer way to saying e.g. `#define VALUE4 1234567`. – Some programmer dude Aug 21 '18 at 09:27
  • so enum identifier { value1=100,value2=30000}; is equal to #define VALUE1 100 #define VALUE2 30000. and 'enum_variable' is a variable, expected to have some random integer constant values? – shaik nisar ahmed Aug 21 '18 at 09:33
  • @shaiknisarahmed The initial value of `enum_variable` depends on where it is defined. It could be *indeterminate* (which might *seem* like random), or it could be zero. – Some programmer dude Aug 21 '18 at 09:40
  • @Someprogrammerdude When we declare a constant like `const int C = 0;` then they will be available at runtime. Instead if we define it like `#define C 0` they are replaced during compilation (preprocessing). Which of these things happens with enum constant `enum Foo { C };`? – Sourav Kannantha B Feb 16 '21 at 12:34
  • 1
    @SouravKannanthaB Constants variables defined at file-scope (outside functions) will have external linkage and thus are available from other translation units, the compiler can't remove its definition. But if the value is known (i.e. in the same translation unit where it was defined) the compiler *can* do compile-time constant-folding (replacing its use with its value) as an optimization. Enumeration values have no storage, they don't exist past the compilation and are therefore more similar to macros. But a little more type-safe than macros. – Some programmer dude Feb 16 '21 at 12:46
1
  1. The enumeration constants, such as value1, are simply numbers. They are not stored in the enum variable itself, but are stored as any other numeric constant: either as part of the machine code itself (segment often called .text), or in a separate read-only segment (segment often called .rodata).

    Enumeration constants are guaranteed to be of type int, but that only matters in the expressions where they are used. How they are actually stored in memory is implementation-defined (depends on system, compiler and linker).

  2. The actual variable can have any size, it is implementation-defined. Though the compiler should pick a size that can fit the largest enumeration constant. Again, it is nothing but a simple integer, it can only hold one number.

In general, enums in C are just "syntactic sugar" on top of plain integers.

Community
  • 1
  • 1
Lundin
  • 195,001
  • 40
  • 254
  • 396