I have a set of values in an enum. As an example I chose WEEK_DAY
(my actual case has many (50+) values, and they are not-continuous (1000,1001,2000,...
)):
typedef enum{
SUNDAY,
MONDAY,
FRIDAY = 6
}WEEK_DAY;
I now want to create a function that, given a WEEK_DAY
value, would return its name. I done this by using:
#define STRING_REPLACE(x) #x
char *value_2_name(WEEK_DAY day)
{
switch(day)
{
case SUNDAY:
return STRING_REPLACE(SUNDAY);
case MONDAY:
return STRING_REPLACE(MONDAY);
case FRIDAY:
return STRING_REPLACE(FRIDAY);
}
}
And calling it with printf("%s", value_2_name(6));
/ printf("%s", value_2_name(FRIDAY));
would print out "FRIDAY" as expected.
Is there a way to squeeze this function into a one line?
i.e. to somehow make the substitution between parameter WEEK_DAY day
and its enum WEEK_DAY
counterpart, and then use the STRING_REPLACE
?
What I'm looking for is something like: STRING_REPLACE(day_2_WEEK_DAY_enum
)
Some enums have forced values so it's not possible to use answers from How to convert enum names to string in c