Thread: enum related basic question

  1. #1
    Registered User sarvsav's Avatar
    Join Date
    Oct 2013
    Posts
    25

    Question enum related basic question

    HI ALL,

    When declaring enum, we use like
    Code:
    enum enumname {
    
    value1 = 1,
    value2 = 2
    
    };

    what's the significance of enumname here? Where it's used?

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    It's the type of the enum, allowing you to create objects which have that enum type. So you can do something like:
    Code:
    enum enumname enum_variable = value1;
    switch(enum_variable)
    {
      case value1:
      ...
    }
    There are a few reasons you'd want to use the enum type instead of an int to hold the enum:
    • It is implicit documentation; "int" is rather generic (could represent anything), but the enum makes your intentions clear
    • Many compilers will be able to warn you if you have a switch() on an enum which does not cover all cases; this helps in tracking down problems

  3. #3
    Registered User sarvsav's Avatar
    Join Date
    Oct 2013
    Posts
    25
    Thanks for your help.

    What i was looking for is some thing like this:

    Code:
    enum _mytype { IamInt = 0,
                             IamFloat = 1,
                             IamUnknown = 2
                           };
    
    struct mystruct {
    
    enum _mytype mytype;
    
    };
    I am sorry, for using bad variable names.

    Thanks again for the help.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    You can think of enum basically as just an integer, it is treated this way by the compiler but there is an implicit mapping along with it. A real life example might be the months. We all know 1 = January, 2 = February, etc. This is a real example of an enumeration. So when you want to come up with your own type of enumerated system, but you don't want to use magic numbers everywhere (after all, 12 can mean "december" but it can also mean 12 dollars or 12 minutes etc.).

  5. #5
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    The OP was asking what significance the NAME has. I don't see any point to it other then self-documenting code. My compiler(gcc) doesn't warn me if I try to assign an enum of one type a value from another enum.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by nonpuz View Post
    My compiler(gcc) doesn't warn me if I try to assign an enum of one type a value from another enum.
    I'll bet that if you turn up the warning level on GCC (-Wall and -Wextra) it will warn you about it, especially if the value you're assigning doesn't exist in both enums.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    A good post on this topic is here:

    enum type check in C/gcc - Stack Overflow

    If you want to type check in gcc, you can do it but it requires quite a bit of boilerplate. The following will produce warnings if you try to drink a food or eat a beverage (type mismatch)

    Code:
    #include <stdio.h>
    
    enum food_type {
        FOOD_UNKNOWN, 
        FOOD_CARROTS, 
        FOOD_CAKE, 
        FOOD_ICECREAM,
    };
    #define FOOD_UNKNOWN    (enum food_type)FOOD_UNKNOWN
    #define FOOD_CARROTS    (enum food_type)FOOD_CARROTS
    #define FOOD_CAKE    (enum food_type)FOOD_CAKE
    #define FOOD_ICECREAM    (enum food_type)FOOD_ICECREAM
    
    enum beverage_type {
        BEVERAGE_UNKNOWN, 
        BEVERAGE_COLA, 
        BEVERAGE_WATER, 
        BEVERAGE_COFFEE,
    };
    #define BEVERAGE_UNKNOWN    (enum beverage_type)BEVERAGE_UNKNOWN
    #define BEVERAGE_COLA        (enum beverage_type)BEVERAGE_COLA
    #define BEVERAGE_WATER        (enum beverage_type)BEVERAGE_WATER
    #define BEVERAGE_COFFEE        (enum beverage_type)BEVERAGE_COFFEE
    
    #define CHECK_TYPE(TYPE, X) \
    ({  TYPE __dummy; \
        typeof(X) __dummy2; \
        (void)(&__dummy == &__dummy2); \
        1; \
    })
    
    #define eat_food(FOOD) do { \
        CHECK_TYPE(enum food_type, FOOD); \
        eat_food_(FOOD); \
    } while(0)
    
    #define drink_beverage(BEVERAGE) do { \
        CHECK_TYPE(enum beverage_type, BEVERAGE); \
        drink_beverage_(BEVERAGE); \
    } while(0)
    
    void eat_food_(enum food_type ft)
    {
        const char *ft_str;
        switch (ft) {
        case FOOD_CARROTS:
            ft_str = "Carrots";
            break;
        case FOOD_CAKE:
            ft_str = "Cake";
            break;
        case FOOD_ICECREAM:
            ft_str = "Ice Cream";
            break;
        default:
            ft_str = "Unknown Foods";
            break;
        }
        printf("You are eating %s\n", ft_str);
    }
        
    void drink_beverage_(enum beverage_type bt)
    {
        const char *bt_str;
        switch (bt) {
        case BEVERAGE_COLA:
            bt_str = "Cola";
            break;
        case BEVERAGE_WATER:
            bt_str = "Water";
            break;
        case BEVERAGE_COFFEE:
            bt_str = "Coffee";
            break;
        default:
            bt_str = "Unknown Beverages";
            break;
        }
        printf("You are drinking %s\n", bt_str);
    }
    
    int main()
    {
        eat_food(FOOD_CARROTS);
        drink_beverage(BEVERAGE_COFFEE);
        
        eat_food(BEVERAGE_WATER); /* wrong type */
        drink_beverage(FOOD_CAKE); /* wrong type */
        return 0;
    }
    The warning generated will stop you because the macro eat_food expands to first call the CHECK_TYPE macro, which will try to compare pointers of distinct types without a cast. Its quite a roundabout way but it might be useful if you have a multitude of different enumerated types and want to keep them separate. Notice however that you have to define the enumeration constants twice (once with the preprocessor, and once without), and the "typeof" keyword is probably only in gcc.
    Last edited by c99tutorial; 11-19-2013 at 04:21 PM. Reason: remove magic numbers

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. A question related to BGI
    By progue in forum Windows Programming
    Replies: 6
    Last Post: 05-25-2008, 10:04 AM
  3. basic questions about enum
    By George2 in forum C# Programming
    Replies: 3
    Last Post: 05-15-2008, 05:58 AM
  4. Question related to enum
    By g_p in forum C Programming
    Replies: 1
    Last Post: 05-09-2007, 03:09 PM
  5. Dos related Question
    By MethodMan in forum A Brief History of Cprogramming.com
    Replies: 31
    Last Post: 07-02-2002, 12:16 PM