How to concatenate, evaluate and stringify macros?

偶尔善良 提交于 2019-12-13 01:47:52

问题


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



来源:https://stackoverflow.com/questions/50593593/how-to-concatenate-evaluate-and-stringify-macros

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!