#include <stdio.h>
/* To shorten example, not using argp */
int main ()
{
enum compass_direction
{
north,
east,
south,
west
};
enum compass_direction my_direction;
my_direction = west;
return 0;
}
In the following code, what is happening internally when the variable
my_direction is assigned west:
enum compass my_direction;
my_direction = west;
If
char name[] = "John";
is assigning four characters to a five character array ('J', 'o', 'h', 'n',
'\0'), what is being assigned to my_direction internally?
In C enumerations belong to integer types. Rach enumerator has type int.
Each enumerated type shall be compatible with char, a signed integer type, or an unsigned integer type. The choice of type is implementation-defined,128) but shall be capable of representing the values of all the members of the enumeration.
(the C Standard).
In your example
enum compass my_direction;
my_direction = west;
my_direction can occupy the same number of bytes as any integer type provided that all enumerators will fit into allocated memory. As west is equal to 3 then the compiler can allocated a byte (char type) to store west in my_directopn.