C Preprocessor concatenation with variable [duplicate]

余生颓废 提交于 2019-12-12 17:11:38

问题


Possible Duplicate:
C preprocessor and concatenation

Is it possible to concatenate a C preprocessor with a variable name?

#define  WIDTH 32

int dataWIDTH;


// dataWIDTH should be interpreted as 'data32'

printf("%d",dataWIDTH);

回答1:


Your use case requires a double-unescaping; using the token pasting (##) operator by itself will just append the name of the preprocessor directive.

#define WIDTH 32

#define _MAKEDATA(n) data##n
#define MAKEDATA(n) _MAKEDATA(n)

int MAKEDATA(WIDTH) = 7;
int _MAKEDATA(WIDTH) = 8;

int main(int argc, char *argv[]) {
    printf("%d\n", data32);
    return 0;
}

yields

$ gcc -E foo.c 
int data32 = 7;
int dataWIDTH = 8;

int main(int argc, char *argv[]) {
    printf("%d\n", data32);
    return 0;
}



回答2:


There's a token pasting operator called ##, read about it for example here:

http://msdn.microsoft.com/en-us/library/09dwwt6y(v=vs.80).aspx



来源:https://stackoverflow.com/questions/13923425/c-preprocessor-concatenation-with-variable

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