cc-preprocessornameof

Is there something like nameof of C# in C for define directive?


I have the classic

    #define SOME_CONSTANT value

Is there a way to get the string "SOME_CONSTANT" like the C# nameof(field)?

EDIT

I have to parse a file looking for some keyword:

#define KEY_A 1
#define KEY_B 2

int foo(char *s)
{
    if (strcmp(s, nameof(KEY_A)) == 0)
        return KEY_A;
    else if (strcmp(s, nameof(KEY_B)) == 0)
        return KEY_B;
    else
        return -1;
}

if i call foo("KEY_A") it should return 1


Solution

  • Since no-one else has posted the answer...

    @palmis, I think this is the solution that you were seeking.

    #define NAMEOF(x) (#x)