I have studied the type enum but haven't found any use for it, can someone elucidate what's enum good for and what does it do??
Someone answer please.
I have studied the type enum but haven't found any use for it, can someone elucidate what's enum good for and what does it do??
Someone answer please.
---Programming is like roaming, you never know where you'll end at------
If 'here' is pronounced as 'hear', why 'there' isnt pronounced as 'dear'??
[email protected]
>can someone elucidate what's enum good for and what does it do??
I think you are talking about enumerate type.
Enumerate Type is good for programers to define
their own data type, unlike built-in data type such as float, int, char..(reserved data type).
Creating enumerate type is usually depend on your
program's data structures or your own design.
>what does it do???
The answer is: what you try do to with it.
Personally, I always used enum to make myself a "boolean" in C.
An enummeration can be used to group an amount of constants together. You can also use enummeration a type, to make clear that a variable should only have the values as given in the enummeration. So it makes code clearer.
Just compare:
/* result is an integer */
int result;
/* result is of type error_e, which is an enum */
error_e result;
Here the type shows that you're dealing with an enummeration type. When you want to know which errors can occur and how much different errors there are, you can just search for type error_e.
enum is also quite handy for defining constant values. Consider:
Similar to using #define, this does save a bit of typing. And again, it is handy for defining your own types. You could have modified the above so it was:Code:enum { Color_Black = 0, Color_Green, Color_White, Color_Red, Color_Blue, Color_Yellow, Color_Grey, Color_Error }; ... stuff goes here ... switch( color ) { case Color_Black: ... break; case Color_Red: ... break; default: Message( "You chose an invalid color." ); }
Quzah.Code:enum Color { ... }; And later: Color getColor( struct someObject o ) { return o.color; }
Hope is the first step on the road to disappointment.