Thread: Enum??

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    75

    Enum??

    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]

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    85
    >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.

  3. #3
    Personally, I always used enum to make myself a "boolean" in C.

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    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.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    enum is also quite handy for defining constant values. Consider:

    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." );
    }
    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 { ... };
    
    And later:
    
    Color getColor( struct someObject o )
    {
        return o.color;
    }
    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A basic question on enum
    By MasterM in forum C++ Programming
    Replies: 2
    Last Post: 06-12-2009, 09:16 PM
  2. enum switchcase and windows message q
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 11-27-2006, 01:16 PM
  3. Conflicting enum issue
    By 7force in forum C Programming
    Replies: 1
    Last Post: 07-05-2006, 03:51 AM
  4. Switch case and enum help
    By SomeCrazyGuy in forum C++ Programming
    Replies: 9
    Last Post: 04-21-2005, 08:53 PM
  5. enum
    By JerryL in forum C++ Programming
    Replies: 5
    Last Post: 02-25-2004, 05:45 PM