An enum is normally defined as enum Colors { black, white };
and has to be referred to by the name enum Colors
(the enum
in front is required). For instance:
enum Colors { black, white };
void print_color(enum Colors color) {
if(color == black) {
printf("Black\n");
} else if(color == white) {
printf("White\n");
}
}
Now, the "typedef" syntax is typedef enum { black, white } Colors;
, and now we just refer to it by the name Colors
(no enum
in front):
typedef enum { black, white } Colors;
void print_color(Colors color) {
if(color == black) {
printf("Black\n");
} else if(color == white) {
printf("White\n");
}
}
This weird syntax is becuase enum { black, white }
is actually a nameless enum, and the typedef
keyword gives this nameless type a name.