C预处理程序与变量的串联 [英] C Preprocessor concatenation with variable
本文介绍了C预处理程序与变量的串联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:
C预处理程序和串联
是否可以将 C 具有变量名的预处理器?
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);
推荐答案
您的用例需要两次转义。单独使用令牌粘贴(##)运算符将只附加预处理程序指令的名称。
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;
}
收益
$ gcc -E foo.c
int data32 = 7;
int dataWIDTH = 8;
int main(int argc, char *argv[]) {
printf("%d\n", data32);
return 0;
}
这篇关于C预处理程序与变量的串联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文