tags:

views:

175

answers:

1

Part 1

In C, is there any difference between declaring an enum like this:

typedef enum{VAL1, VAL2,} firstEnum;

and like this:

enum secondEnum{Val1, Val2,};

Apart from the fact that when using secondEnum, you have to write:

enum secondEnum...;

Part 2

Also, am I right in thinking that the following is equivalent:

enum{Val1, Val2,} enum1;

and

enum thirdEnum{Val1, Val2,}
enum thirdEnum enum1;

Thanks

+3  A: 

In part 1, there is obviously a difference - first you are declaring firstEnum as a typedef for the (anonymous) enumerated type, while in the second secondEnum is the tag for the enumerated type and there is not a typedef involved. The first is recommended for the ease of use as you have noted.

In part 2, the two are not equivalent - the first declares an anonymous enumerated type and defines enum1 to be of that type. The second declares a named enumerated type and then declares enum1 to be of that type. The significance is that you can use the named type in other parts of the code, while in the first you cannot use it anywhere else so you will probably have to use integer values as alias for the values of the enumerated type.

1800 INFORMATION
Ok, the question (which on re-read was not entirely clear) was meant to be from a functional point of view. I.e. in part 1, if I was to create an enum of type 'firstEnum' and another enum of type 'secondEnum' would they be equivalent (yes).The same goes for part 2, are the enum1s declared equivalent in terms of functionality (again yes)
Lehane
I don't think I really know what you mean by "functionally equivalent". None of the samples will actually generate any code by themselves so it's hard to say they will cause the computer to act differently in any fashion. That question (to me) isn't so interesting anyway, most trivial examples of use of the enumerated types would behave pretty much the same no matter what. The interesting behaviour is how they cause the compiler to act and the effect it has on the client code and the consumer of the code
1800 INFORMATION