I don't know the exact logic of your implementation, so I won't go into that discussion. You can use this :
#define CONV2STRING(X) #X
#define TO_STRING(X) CONV2STRING(X)
#define EXPAND(Y) Y
#define MERGER( X, Y) X##Y
#define MERGE( X, Y) MERGER( X, Y)
#define SAY_HELLO HELLO
#define SAY_BYE BYE
typedef enum {
HELLO0BYE = 0,
HELLO1BYE = 1,
HELLO2BYE = 2,
HELLO3BYE = 3,
HELLO4BYE = 4,
HELLO5BYE = 5,
} enum_test;
const static struct {
enum_test value;
const char *str;
} conversion [] = {
{HELLO0BYE, "HELLO0BYE"},
{HELLO1BYE, "HELLO1BYE"},
{HELLO2BYE, "HELLO2BYE"},
{HELLO3BYE, "HELLO3BYE"},
{HELLO4BYE, "HELLO4BYE"},
{HELLO5BYE, "HELLO5BYE"},
};
int StringToEnum (const char *str) {
int j;
for (j = 0; j < sizeof (conversion) / sizeof (conversion[0]); ++j)
if (!strcmp (str, conversion[j].str))
return conversion[j].value;
return -1;
}
int main(int argc, char** argv)
{
enum_test output;
#define INPUT 5
output = StringToEnum(TO_STRING (MERGE(MERGE( SAY_HELLO, EXPAND(INPUT)), SAY_BYE)));
printf( "Macro Expansion : %s\n", TO_STRING (MERGE(MERGE( SAY_HELLO, EXPAND(INPUT)), SAY_BYE)));
printf("output = %d\n", output);
#undef INPUT
#define INPUT 4
output = StringToEnum(TO_STRING (MERGE(MERGE( SAY_HELLO, EXPAND(INPUT)), SAY_BYE)));
printf( "Macro Expansion : %s\n", TO_STRING (MERGE(MERGE( SAY_HELLO, EXPAND(INPUT)), SAY_BYE)));
printf("output = %d\n", output);
#undef INPUT
}
Note: I found the elegant conversion from string to enum here.