What is the size of an enum in C?

3

In general, the enum stores an integer, can I consider that this is the size of it?

    
asked by anonymous 26.01.2017 / 11:42

1 answer

4

If you want to know the size with security the best thing to do is to use sizeof , even because the int itself does not have maximum guaranteed size, then it can vary.

If you want to get an idea of the size occupied, you can assume that normal is to be int .

Some compilers have extensions that allow you to work with different sizes (for example with __attribute__((packed)) ), but within normality a data that contains an enumeration actually contains only a int , and it is best not to escape this pattern.

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

Note that there is no space occupied by the enumeration definition. So struct or union , this is a code declaration for the compiler to know how to generate the correct formula to access 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 possible to create this kind of thing, the programmer must turn around to organize and access memory the way it needs, this concept is abstract.

    
26.01.2017 / 11:42