You've provided a C-style enumeration (enum) for seasons, along with two arrays:
- An array of the
enum season values.
- An array mapping each season to its string representation.
Here's a breakdown and explanation of your code:
🌸 Enumeration Definition
enum season {
SPRING,
SUMMER,
AUTUMN,
WINTER
};
- This defines an enumeration type
enum season with four values.
- By default, the first value (
SPRING) is assigned 0, and each subsequent value increments by 1:
SPRING = 0
SUMMER = 1
AUTUMN = 2
WINTER = 3
📦 Array of All Seasons
static const enum season SEASONS[] = {
SPRING,
SUMMER,
AUTUMN,
WINTER
};
- This creates a constant array of all the
enum season values.
- Useful for iterating over all seasons in code.
📝 Array Mapping Seasons to Strings
static const char* SEASON_NAMES[] = {
[SPRING] = "spring",
[SUMMER] = "summer",
[AUTUMN] = "autumn",
[WINTER] = "winter",
};
✅ Example Usage
Here's how you might use this in a full C program:
#include <stdio.h>
enum season {
SPRING,
SUMMER,
AUTUMN,
WINTER
};
static const enum season SEASONS[] = {
SPRING,
SUMMER,
AUTUMN,
WINTER
};
static const char* SEASON_NAMES[] = {
[SPRING] = "spring",
[SUMMER] = "summer",
[AUTUMN] = "autumn",
[WINTER] = "winter",
};
int main() {
for (int i = 0; i < 4; i++) {
enum season s = SEASONS[i];
printf("Season %d: %s\n", s, SEASON_NAMES[s]);
}
return 0;
}
Output:
Season 0: spring
Season 1: summer
Season 2: autumn
Season 3: winter
🔒 Notes
- The use of
static const makes these arrays internal to the translation unit (i.e., file scope), which is good practice for encapsulation.
- Designated initializers like
[SPRING] = "spring" are part of C99 and later standards. If you're using C++, this syntax won't work — you'll need a different approach (like a switch or std::array with an enum class).
Let me know if you'd like a C++ version or a way to convert strings back to enums!