enum

With regards to C, not C++.

. . .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#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?
Last edited on
closed account (zb0S216C)
divine fallacy wrote:
"what is happening internally when the variable
my_direction is assigned west:

enum compass my_direction;
my_direction = west;
"

Nothing of note. The assignment is the same as assigning some arbitrary integer to some integral variable.

divine fallacy wrote:
"char name[] = "John";

is assigning four characters to a five character array
"

No, it's initialising a 5-character array with 5 characters.

These are basic questions and the answers can be found with a simple Google search.

Wazzak
Last edited on
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.
Topic archived. No new replies allowed.