本文介绍了C语言程序设计:preprocessor,宏作为标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图做一些事情,在概念上类似于此,但似乎无法得到它的工作(在末尾显示错误)的任何想法?
的#include<&stdio.h中GT;INT主(INT ARGC,字符常量*的argv [])
{
INT abc_def_ghi = 42;
#定义后缀GHI
#定义VAR(preFIX)preFIX ## _高清_ ##后缀
的printf(%d个\\ N,VAR(ABC));
返回0;
}//无标题:8:错误:abc_def_SUFFIX'未申报(在一次使用此功能)
解决方案
您只需要额外的间接:
的#include<&stdio.h中GT;INT主(INT ARGC,字符常量*的argv [])
{
INT abc_def_ghi = 42;
#定义后缀GHI
#定义VAR3(preFIX,后缀)preFIX ## _高清_ ##后缀
#定义VAR2(preFIX,后缀)VAR3(preFIX,后缀)
#定义VAR(preFIX)VAR2(preFIX,后缀)
的printf(%d个\\ N,VAR(ABC));
返回0;
}
尽管它看起来多余的,其实并非如此。
I'm trying to do something that is conceptually similar to this, but can't seem to get it to work (error shown at end) any ideas?
#include <stdio.h>
int main( int argc , char const *argv[] )
{
int abc_def_ghi = 42;
#define SUFFIX ghi
#define VAR(prefix) prefix##_def_##SUFFIX
printf( "%d\n" , VAR(abc) );
return 0;
}
// untitled:8: error: ‘abc_def_SUFFIX’ undeclared (first use in this function)
解决方案
You just need additional indirection:
#include <stdio.h>
int main( int argc , char const *argv[] )
{
int abc_def_ghi = 42;
#define SUFFIX ghi
#define VAR3(prefix, suffix) prefix##_def_##suffix
#define VAR2(prefix, suffix) VAR3(prefix, suffix)
#define VAR(prefix) VAR2(prefix, SUFFIX)
printf( "%d\n" , VAR(abc) );
return 0;
}
Even though it looks redundant, it's not.
这篇关于C语言程序设计:preprocessor,宏作为标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!