Перечисления

Инициализация типов

enum PreciousMetal {
    Platinum, Gold, Silver
};

//the values can be set explicitly, the next ones go on from there
enum Planet {
    Mercury = 1, Venus, Earth
};

//typedef spares the word enum; C23 can fix the underlying type
typedef enum : unsigned char {
    Summer, Fall, Winter, Spring
} Season;

enum PreciousMetal gold = Gold;
//gold is 1
enum Planet earth = Earth;
//earth is 3
Season fall = Fall;
//fall is 1
size_t seasonSize = sizeof fall;
//seasonSize is 1

//an enum value is a number: its name comes from an array of strings
const char *seasonNames[] = {"Summer", "Fall", "Winter", "Spring"};
//seasonNames[fall] is "Fall"

printf("gold is %d\n", gold);
printf("earth is %d\n", earth);
printf("fall is %d\n", fall);
printf("seasonSize is %zu\n", seasonSize);
printf("seasonNames[fall] is %s\n", seasonNames[fall]);