8

Is it legal to assign an int to an enum type as shown in c.color = 1? I ran this code and it does seem to set c.color to BLUE as BYE is printed, but I wanted to understand if this actually sets the enum correctly.

typedef enum {
   GREEN = 0,
   BLUE
}COLOR;

typedef struct{
    COLOR color;
}COLORS;

int main()
{
    COLORS c;
    c.color = 1;
    if(c.color == BLUE)
    {
       printf("BYE");
    }
}
Smiley7
  • 185
  • 1
  • 3
  • 10
  • The short answer is 'Yes'. The `enum`, in addition to being an *integer type*, creates *global constants* that can, in return, be used to set the enum to any one of the values using the convenient constant as opposed to using a *magic number*, e.g. `enum direction { NORTH, WEST, SOUTH, EAST }; direction = SOUTH; switch direction { case NORTH: /* do suff */ break; case WEST: /* do stuff */ break; ...` You can use an `enum` for the purpose of declaring global constants independent of whether you use the `enum` later in your code, e.g. `enum { MAXC = 128, MAXN = 512 };` – David C. Rankin Aug 10 '17 at 02:26
  • Related topic: [How to create type safe enums?](https://stackoverflow.com/questions/43043246/how-to-create-type-safe-enums). – Lundin Aug 10 '17 at 06:44

4 Answers4

7

An enum is considered an integer type. So you can assign an integer to a variable with an enum type.

From section 6.2.5 of the C standard:

16 An enumeration comprises a set of named integer constant values. Each distinct enumeration constitutes a different enumerated type .

17 The type char , the signed and unsigned integer types, and the enumerated types are collectively called integer types . The integer and real floating types are collectively called real types .

dbush
  • 205,898
  • 23
  • 218
  • 273
1

From the CPP Reference website,

Each enumerated type is compatible with one of: char, a signed integer type, or an unsigned integer type. It is implementation-defined which type is compatible with any given enumerated type, but whatever it is, it must be capable of representing all enumerator values of that enumeration.

You should be fine assigning an enum an integer value.

TimD1
  • 982
  • 15
  • 26
1

You can set an enum this way, but the preference would be to just use "BLUE" rather than "1". Using a specific number is called a "Magic Number", and makes the code harder to understand and maintain. When you assign it directly with the enum name, someone coming back to the code later (including yourself weeks or months from now) will hopefully be able to immediately understand what the code is doing without having to look up what "1" means.

It might seem simple and intuitive with this little code, but once you have many files and layers to your code, writing readable, easily understandable code gets a lot more important.


Also, you can cast the int if you want to still use the number, but also ensure correct typing. e.g.

c.color = (COLOR)1;
LightCC
  • 9,804
  • 5
  • 52
  • 92
0

According to C99 standard, enumerated types are considered integer types:

The type char, the signed and unsigned integer types, and the enumerated types are collectively called integer types.

The standard allows assignments of integers to variables of enumerated types, and specifies that a compiler should issue a warning when this happens:

An implementation may generate warnings in many situations, none of which are specified as part of this International Standard. The following are a few of the more common situations.

...

[When] a value is given to an object of an enumerated type other than by assignment of an enumeration constant that is a member of that type, or an enumeration object that has the same type, or the value of a function that returns the same enumerated type (6.7.2.2).

Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523