1

What is the difference between the following in C language:

typedef enum month_t
{
jan,
feb,
march
}month;

AND

typedef enum
{
monday,
tuesday,
wednesday
}day;

Before posting this question I read this : What is a typedef enum in Objective-C?

But did not understand quite well...

Community
  • 1
  • 1
GuiccoPiano
  • 146
  • 1
  • 4
  • 12
  • 4
    One defines months, the other days. – Kerrek SB May 24 '13 at 13:13
  • 1
    In the first case, you could have left off the `month_t` and then the two would be the same way of doing it. The `month_t` is just another type tag in C one can use, so you could declare a month as `enum month_t my_month;` or just `month my_month;`. For day, you can only do `day my_day;` the way it's currently defined. – lurker May 24 '13 at 13:18
  • @KerrekSB: That is not actually a difference in the C language; there is an isomorphism between them. It is only a difference outside the C language, to its users. – Eric Postpischil May 24 '13 at 13:21

1 Answers1

11

The first also introduces an enum tag, which means the enumeration can be used like this:

enum month_t first = jan;
/* or */
month second = feb;

the second doesn't, so the enumeration is only available with the typedef:ed name day.

Also, of course, the enumerations themselves are different, but that's kind of obvious.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • 2
    Yes (upvoted). Putting it another way, the first declares an enum called 'month_t' and typedefs an alias called 'month'. The second declares an anonymous enum and types an alias called 'day'. – davmac May 24 '13 at 13:18