Click here to Skip to main content
15,888,264 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all, i want to know the difference between following two statements.

typedef enum COLOR{
RED,
WHITE
};
and
typedef enum {
RED,
WHITE
}COLOR;
Posted
Updated 7-Aug-14 23:50pm
v2
Comments
Andreas Gieriet 8-Aug-14 5:16am    
Did you try to compile?
Andi

Assuming you replace the 0 and 1 with legal enumerator names, then the first is a redundant typedef (you can leave it away) and the second defines an alias for an otherwise unnamed enum type.
Cheers
Andi
 
Share this answer
 
Comments
Stefan_Lang 8-Aug-14 5:50am    
Since the question is tagged as C++, it would be fair to say that the typedef as such is nonsensical. but of course your answer is entirely correct. Have a 5
Andreas Gieriet 8-Aug-14 7:23am    
Thanks for your 5!
Cheers
Andi
Both are invalid statements in C++ (as well in C).
Please see: "enumeration declaration"[^].
 
Share this answer
 
Yes, both are compiling and working fine. I just want to know is any difference between them ??
 
Share this answer
 
Comments
Andreas Gieriet 8-Aug-14 5:42am    
They won't compile. 0 and 1 are not legal enumerators.
Andi
Stefan_Lang 8-Aug-14 5:43am    
1. Use the correct [Reply] button at the top right of the comment if you want to reply to a comment. Posting a solution means you have a solution to the problem - which you have not!

2. the code that you posted doesn't compile. if you do have code that compiles, copy/paste that code. Use the green [improve question] button at the bottom right of your posting to correct it.
sandeepyes 8-Aug-14 5:47am    
oh sorry, I just put those 0,1 as example. My question is what is the difference if the name ABCD comes before and after the braces.

mean to say what is the difference between
typedef enum ABCD{
RED,
WHITE
};

and
typedef enum{
RED,
WHITE
}ABCD;
Stefan_Lang 8-Aug-14 6:13am    
In the first, the typedef is nonsensical since you did not specify the symbol you want to bind the typedef to - it's like writing "int;", i. e. a variable declaration without a variable.

In the second, the presence of the typedef changes the meaning of ABCD from "variable of type unnamed enum{RED,WHITE}" to "typename used as alias for the unnamed enum{RED,WHITE}"

Both versions will be usable in exactly the same way. But, either way, you should just write "enum ABCD{...}" instead, omitting the redundand typedef.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900