1

Although I have experience with Enums in Java, typedef and Objective-C / C style enums are new to me. I can't seem to grasp the difference between enums with the names declared in the beginning and enums with the names not declared. For example:

typedef enum Months {JAN, FEB, MAR, APR}
    Months;

vs.

typedef enum {JAN, FEB, MAR, APR}
    Months;

is there a reason to ever pick the former form over the latter? The former seems to be redundant by stating 'Months' twice.

This question Looks very similar but it doesn't have the name at the end so I don't know if it is answering the same question.

Community
  • 1
  • 1
ThinkBonobo
  • 15,487
  • 9
  • 65
  • 80
  • Generally speaking, the compiler's okay with either one, but in my experience many libraries use the first option for clarity purposes. Also, it's `typedef`, rather than `typdef` – BrainSteel Sep 30 '13 at 18:50
  • 1
    It should also be noted that you should use typedef NS_ENUM(type, enumName) for compile-time enum type safety. – paulrehkugler Sep 30 '13 at 21:03
  • I don't understand how this is a duplicate. The accepted answer suggests that we use the former over the latter or the macro as suggested by @paulrehkugler but I was asking why you would ever use the former over the latter which I am still not sure of after reading the other article. Is it just for historical significance? – ThinkBonobo Oct 03 '13 at 15:21

1 Answers1

0

On a general basis

typedef enum <tagname 1>
{
//values

}<tagname2>;

<tagname 1> is used to define more clarity about what your enum is all about

while <tagname2> is just a reference name which contains the whole reference of typedef enum <tagname 1>

compiler supports both of the variations

Sohil Omer
  • 1,171
  • 7
  • 14
  • Thanks cyberworm, one question though regarding your response: How does provide more clarity? Are they ever used with different names? – ThinkBonobo Oct 03 '13 at 15:23
  • as per the guidelines you have to tagname1 is used to give more meaning ful information about the enum – Sohil Omer Oct 03 '13 at 15:26