5

The standard says that an enum type is a integral type between char, signed and unsigned.

But an int into a 32 bit machine should be 4 byte and into a 64 bit machine should be 8 byte. So why my GCC into a 64 bit machine returns 4 as sizeof of this enum?

enum color
{
  RED,
  GREEN,
  BLUE
};

size_t t = sizeof (enum color); // here 4
xdevel2000
  • 20,780
  • 41
  • 129
  • 196
  • 1
    An the `sizeof(int)` isn't 4 on your machine? – M Oehm Oct 03 '14 at 13:05
  • Related: [type of enums](http://stackoverflow.com/questions/1113855/is-the-sizeofenum-sizeofint-always) and [size of int](http://stackoverflow.com/questions/10197242/what-should-be-the-sizeofint-on-a-64-bit-machine) – Adriano Repetti Oct 03 '14 at 13:06
  • 1
    On all common 64 bit ABIs `int` is 32 bit. –  Oct 03 '14 at 13:07
  • 3
    The size of an `int` is determined by a compiler. It is not necessarily the same as the word size of the machine. – Klas Lindbäck Oct 03 '14 at 13:08
  • The C Standard on this had been quoted here: http://stackoverflow.com/a/1113869/694576 – alk Oct 03 '14 at 13:10
  • Your question description is full of false premisses. Another one: the standard doesn't mention `signed` or `unsigned`, but it speaks of signed or unsinged integer types. Also there is no thing like *the* word size on a modern machine. They have different types of registers with different width. The description of the ABI interface of modern architectures is complex. Voting to close this question, this leads nowhere. – Jens Gustedt Oct 03 '14 at 13:18
  • `int` in 16-bit machine is commonly 16-bit while in 32-bit and 64-bit machine, it's commonly 32-bit. the size of `long` should be a better measure of the bit length of the machine but still not accurate. – Jason Hu Oct 03 '14 at 13:29
  • @HuStmpHrrr: `sizeof(long)` is a very bad idea, because LLP64 is used is some platforms (like Windows, and at least one OS in the Unix family). `sizeof(void*)` (or `sizeof(uintptr_t)` for C99) is a far better measure. – Tim Čas Apr 02 '15 at 10:34

1 Answers1

4

OP: The standard says that an enum type is a integral type between char, signed and unsigned.
A: Close, but not quite. See more @alk

Each enumerated type shall be compatible with char, a signed integer type, or an unsigned integer type. The choice of type is implementation-defined, but shall be capable of representing the values of all the members of the enumeration. C11dr §6.7.2.2 4

OP: But an int on a 32 bit machine should be 4 bytes and on a 64 bit machine should be 8 bytes.
A: No. Although common, a processor's word size and int are usually the same, the C spec does not require that and many implementations do not follow that especially with compilers on 64-bit machines using 32-bit int. Also 8-bit processors (still common in 2014 in the embedded world) would need at least an 16-bit int to be compliant.

OP:why does GCC on a 64 bit machine return 4 as sizeof of this enum?
A: It's the compiler's choice. Likely to match an int size, fairly common with 64-bit compilers.

Community
  • 1
  • 1
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • 1
    His compiler is using LP64 programming model I think. And in it, int is of 32-bits if I am not wrong. – Mazhar Nov 24 '16 at 08:53