How big is an "Enum" in C?

Asked

Viewed 322 times

3

In general the enum keeps a whole, I can consider that this is the size of it?

1 answer

5


If you want to know the size safely the best thing to do is to use the sizeof, even because the int has no maximum size guaranteed, so it can vary.

If you want to have a busy size idea, you can consider that the normal is to be a int.

Some compilers have extensions that allow working with different sizes (for example with __attribute__((packed))), but within the norm a data containing an enumeration actually contains only one int, and it is better not to run away from this pattern.

So much so that it’s common to use enum to create integer value constants. This form has advantages over the const and to the #define.

Note that there is no space occupied by the enumeration definition. As well as struct or union, this is a code statement for the compiler to know how to generate the correct formula for accessing the data in memory, but it disappears after the machine code is generated, only the data will exist in memory. These statements serve to guide the programmer better. In Assembly it is not even possible to create this kind of thing, the programmer must turn to organize and access the memory the way he needs, this concept is abstract.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.