How to concatenate, evaluate and stringify macros?

2019-08-02 10:39发布

问题:

I am trying to stringify the substitution (evaluation) of a macro concatenation. For example:

#include <stdio.h>

#define FOO_ONE 12
#define FOO_TWO 34
#define BAR_ONE 56
#define BAR_TWO 78

#define MAKE_MAC(mac) // ... what to do here?

void main(int argc, char *argv[])
{
    printf("FOO: " MAKE_MAC(FOO) "\n");
    printf("BAR: " MAKE_MAC(BAR) "\n");
}

The result I am seeking is:

FOO: 1234
BAR: 5678

I tried a few forms, I think the best attempt is this:

#define STRINGIFY(mac) #mac
#define CONCAT(mac1, mac2) STRINGIFY(mac1 ## mac2)
#define MAKE_MAC(mac) CONCAT(mac, _ONE) CONCAT(mac, _TWO)

But, it only gets me this far:

FOO: FOO_ONEFOO_TWO
BAR: BAR_ONEBAR_TWO

So, how can I add the extra step of evaluation of the resulting concatenated macro, before it gets stringified?

回答1:

Try this:

#include <stdio.h>

#define FOO_ONE 12
#define FOO_TWO 34
#define BAR_ONE 56
#define BAR_TWO 78

#define STRINGIFY(arg) #arg
#define CONCAT(arg1, arg2) STRINGIFY(arg1) STRINGIFY(arg2)

#define MAC(arg) CONCAT(arg##_ONE, arg##_TWO)

int main(){

    printf("FOO: " MAC(FOO) "\n");
    printf("BAR: " MAC(BAR) "\n");

    return 0;
}

My output:

FOO: 1234
BAR: 5678


回答2:

You need to defer stringification by introducing a layer of indirection:

#define STRINGIFY_X(x) #x
#define STRINGIFY(x) STRINGIFY_X(x)
#define MAKE_MAC(mac) STRINGIFY(mac ## _ONE) STRINGIFY(mac ## _TWO)

live example on wandbox.org