It's up to the implementation how big an enum
is. From section 6.7.2.2p4 of the C standard regarding Enumeration specifiers:
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. The enumerated type is incomplete until
immediately after the }
that terminates the list of enumerator
declarations, and complete thereafter.
So the size of an enum
type could be the size of an int
, char
, or any other integer type. As an example, gcc sizes enums as follows:
Normally, the type is unsigned int
if there are no negative values in
the enumeration, otherwise int
. If -fshort-enums
is specified, then if
there are negative values it is the first of signed char
, short
and
int
that can represent all the values, otherwise it is the first of
unsigned char
, unsigned short
and unsigned int
that can represent all
the values.
As for the values of each member, paragraph 3 states:
The identifiers in an enumerator list are declared as
constants that have type int
and may appear wherever such
are permitted. An enumerator with
=
defines its enumeration constant as the value of the constant expression. If the first enumerator has no
=
, the value of its enumeration constant is 0. Each subsequent enumerator with no
=
defines its enumeration constant as the value of the constant expression obtained by adding 1 to the value of the
previous enumeration constant. (The use of enumerators with
=
may produce enumeration constants with values that duplicate other values in the same enumeration.) The enumerators of an enumeration
are also known as its members.
In this case, the constant ENUM_5
is explicitly assigned the value 0xFFFFFFF0
, so ENUM_6
is 0xFFFFFFF
, ENUM_7
is 0xFFFFFFF2
, and ENUM_8
is 0xFFFFFFF3
. Each of these constants needs 4 bytes to store the value, so this particular enum
must be at least 4 bytes in size, although it could be larger.
Regarding:
How it saves all of its enum member values within 4 bytes?
It doesn't hold all 4 members at once. An enum can hold one of those 4 members. You're defining a type, not a value. For example:
enum enumStruc_2 e1 = ENUM_5;
enum enumStruc_2 e2 = ENUM_6;