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
In part 1, there is obviously a difference – first you are declaring
firstEnumas atypedeffor the (anonymous) enumerated type, while in the secondsecondEnumis 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
enum1to be of that type. The second declares a named enumerated type and then declaresenum1to 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.