正确的顺序#define宏在C / C ++中

时间:2016-05-11 19:13:39

标签: c c-preprocessor

我尝试保存变量名称,如下所示:

#define TEST someVariable
#define makeString(x) #x

int main(int argc, char *argv[])
{
    char TEST[] = makeString(TEST);
    printf("%s    %s\n", TEST, makeString(TEST));
    return 0;
}

输出是:

appden4ik@appDen4ik:/opt/preprocessor/Debug$ ./preprocessor 
TEST    TEST'

我想要下一个输出:

someVariable      someVariable

我哪里错了?

1 个答案:

答案 0 :(得分:0)

#include <stdio.h>


#define TEST variable
#define makeStr(x) #x
#define makeString(x) makeStr(x)

int main(int argc, char *argv[]) {
    char TEST[] = makeString(TEST);
    printf("%s    %s\n", TEST, makeString(TEST));
    return 0;
}

这有帮助