6

What is the difference between

enum week{Mon, Tue, Wed, Thur, Fri, Sat, Sun};
enum week day = Wed; 

and

enum week{Mon, Tue, Wed, Thur, Fri, Sat, Sun};
int day = Wed;

in C?

I couldn't find what is the benefit of using variables of type enum over using a regular int.

Amirreza A.
  • 736
  • 4
  • 10
  • 1
    ... Readability ... – Alex Lop. Aug 04 '20 at 14:49
  • `sizeof(enum week)` might be less than `sizeof(int)`. The C spec says that an enumerated type will be compatible with `char`, a signed integer type, or an unsigned integer type. I suppose the compiler _could_ make the type wider than `int`, but since all the enumerated constants are type `int`, there is no need for it to be wider than `int`. – Ian Abbott Aug 04 '20 at 14:56

3 Answers3

9

The benefit is that using an enum make your intentions clearer, both to people reading your code and to the compiler. For example, any half-way decent compiler will warn you if you use an enum with incomplete switch cases:

switch (day) {
    case Mon: printf("Monday\n"); break;
    case Tue: printf("Tuesday\n"); break;
}

Here GCC (with -Wall) emits:

warning: enumeration value 'Wed' not handled in switch [-Wswitch]

7 |     switch (day) {
  |     ^~~~~~

If the type of day was int, you wouldn’t get this very helpful warning.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
2

The identifiers/constants of the enum have type int, so it is permissible to assign the values of them to int variables:

"The identifiers in an enumerator list are declared as constants that have type int and may appear wherever such are permitted."

C18, §6.7.7.2/3

However, the type of the enumeration can vary per implementation:

"Each enumerated type shall be compatible with char, a signed integer type, or an unsigned integer type. The choice of type is implementation-defined,131) but shall be capable of representing the values of all the members of the enumeration."

C18, §6.7.7.2/4

So, sticking with enum type might prevent any kind of type issues when dealing with enums beside being more readable.


Another thing is that it is easily possible to assign the constant of another enum type to day, if it has type int.

enum week {Mon, Tue, Wed, Thur, Fri, Sat, Sun};
enum month {Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dez};

int day = Mar;

This compiles with no issues.

A high-leveled compiler can throw a warning for this when using type enum week for day instead and adds with this an extra layer of security.

enum week {Mon, Tue, Wed, Thur, Fri, Sat, Sun};
enum month {Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec};

enum week day = Mar;

For example, Clang, 10.0 throws without any additional flag:

"warning: implicit conversion from enumeration type 'enum month' to different enumeration type 'enum week' [-Wenum-conversion]"

GCC, 10.1 needs the -Wall or -Wenum-conversion flag to emit the same warning.


Also this post covers a similar concern, but I wouldn't go so far as declaring it as duplicate:

Furthermore, I've found this interesting article:

1

Enumerations in C are a convenient way to create symbolic constants for small sets of things that aren't necessarily ordered.

They're not a high-level abstraction in C the way they are in Java or C#. They're integer types under the hood, and because C trusts that you know what you're doing at all times and never, ever make a mistake it doesn't complain when you mix and match enum and int types.

John Bode
  • 119,563
  • 19
  • 122
  • 198