Unnamed enum & typedef?

I was a little confused with typedef / enum until I realized that I didn't need to call emun. Are there any differences / advantages between the two when used with typedef, the second seems a lot easier for me to understand.

First example:

typedef enum enumMenuItems {
    none,
    add,
    save,
    load,
    list,
    remove
} menuItems;

menuItems optionSelect = none;

Second example:

typedef enum {
    Earth = 1, 
    Mars,
    Saturn,
    Neptune,
    Jupiter
} planets;

planets closest = Mars;

.

EDIT:

typedef enum enumMenuItems {
    none,
    add,
    save,
    load,
    list,
    remove
} menuItems;

Thus, the above essentially defines two types: one enumeration called enumMenuItems, and the second a typedef enumMenuItems called menuItems.

menuItems optionSelect = save;
enum enumMenuItems optionSelect = save;

The above two declarations are essentially the same: one uses a typedef, and the other enum. Therefore, if you use typedef, you can leave your enum unnamed as a type that can be accessed through typedef menuItem.

Gary

+3
source share
1 answer

enum enumMenuItems. :

enum enumMenuTimes optionSelect = none;

, , :

enum _planets { 
    Earth = 1, 
    Mars, 
    Saturn,
    Neptune,
    Jupiter
};
typedef enum _planets planets;

enum _planets planet1 = Earth;
planets       planet2 = Mars;

. - ; , typedef, . , , :

enum {
    Value0,
    Value1,
    Value2
};

, . .

comp.lang.c FAQ.

+5

Source: https://habr.com/ru/post/1724316/


All Articles