You can't do exactly what you want, since by the time the compiler gets the pre-processor output, the "ONE" token is long gone. However, if your goal is to write the list of constants once but generate both tokens and strings, then this can be done.
First, use a macro to build the constants as enums in a header file. File enums.h:
#ifndef ENUMS_H
#define ENUMS_H
#ifndef ENUM
#define ENUM(name,val) enum { name = val };
#endif
ENUM(ONE,1)
ENUM(TWO,2)
ENUM(THREE,3)
#endif /* ENUMS_H */
Second, redefine the macro in a .c file to create a string/integer mapping and include the .h file in the right place. File enums.c:
#include
#include
typedef struct {
char *str;
int val;
} DescriptiveEnum;
static DescriptiveEnum enums[] = {
#define ENUM(name,val) { #name, val },
#include "enums.h"
};
#define NUM_ENUMS (sizeof(enums)/sizeof(enums[0]))
char *enum_to_str(int val)
{
int i;
for (i=0;i<NUM_ENUMS;i++) {
if (enums[i].val == val) return enums[i].str;
}
return "";
}
Now both the enum constants and the mapping function are available to callers. File main.c:
#include <stdio.h>
#include <stdlib.h>
#include "enums.h"
char *enum_to_str(int val);
int main(int argc, char *argv[])
{
int val;
val = ONE;
printf("%d %s\n",val,enum_to_str(val));
return EXIT_SUCCESS;
}