在功能模型界面头文件
中的以下代码段中#define fmiPaste(a,b) a ## b
#define fmiPasteB(a,b) fmiPaste(a,b)
#define fmiFullName(name) fmiPasteB(MODEL_IDENTIFIER, name)
为什么首先使用嵌套宏?为什么不呢
#define fmiPasteB(a,b) a ## b
#define fmiFullName(name) fmiPasteB(MODEL_IDENTIFIER, name)
答案 0 :(得分:0)
它与C
扩展宏的方式有关。
如果你没有嵌套的宏,那么传递给fmiFullName
的文字符号将被字符串化并用于连接,而不是扩展符号。
考虑下面的示例程序:
#include <stdio.h>
#define MODEL_IDENTIFIER Prefix
#define fmiPaste(a,b) a ## b
#define fmiPasteB(a,b) fmiPaste(a,b)
#define fmiFullName(name) fmiPasteB(MODEL_IDENTIFIER, name)
int main(){
int Prefix1 = 2;
int Prefix2 = 3;
printf("%d\n", fmiFullName(1));
printf("%d\n", fmiFullName(2));
#undef fmiPasteB
#undef fmiFullName
#define fmiPasteB(a,b) a ## b
#define fmiFullName(name) fmiPasteB(MODEL_IDENTIFIER, name)
int MODEL_IDENTIFIER1 = 100;
int MODEL_IDENTIFIER2 = 1000;
printf("%d\n", fmiFullName(1));
printf("%d\n", fmiFullName(2));
}
输出:
int MODEL_IDENTIFIER1 = 100;
int MODEL_IDENTIFIER2 = 1000;
printf("%d\n", fmiFullName(1));
printf("%d\n", fmiFullName(2));