What you have is already a common way to do it. I'd recommend to keep it this way and just actually use your macros in the case
statements instead of the magic chars.
If your values are contiguous, you could also use a lookup table like this:
static const char *get_event_string(unsigned char event)
{
static const char *const names[] = {
"ABC",
"DEF",
"XYZ"
};
return names[event - '1'];
}
Both approaches assume that the functions are never called with invalid parameters.
If you can change the values of your events to natural numbers like e.g.
#define ABC 1
#define DEF 2
#define XYZ 3
or maybe even using an enum
:
enum event
{
EV_NONE,
EV_ABC,
EV_DEF,
EV_XYZ
};
then a lookup table will have a real benefit: You don't need a function any more. Just define it (as an example for the enum
above):
const char *const event_strings[] = {
"EV_NONE",
"EV_ABC",
"EV_DEF",
"EV_XYZ"
};
And all you have to write in code to access the name is event_strings[event]
.
Doing it this way even enables you to use the preprocessor to automate defining a matching table to your enum
like shown in this answer.