是否可以通过这种方式在宏内部调用宏:
#include <stdio.h>
#define AA(a1, a2) a1, 3, 5, a2
#define BB(x, y1, y2, y3, y4) { printf("%d %d %d %d %d\n",x, y1, y2, y3, y4 ); }
int main ()
{
int n = 21, k= 11;
BB(31, AA(n,k));
}
此代码在编译中返回 followinf 错误:
test_macro.c: In function ‘main’:
test_macro.c:9:18: erreur: macro « BB » requiert 5 arguments, mais seulement 2 ont été passés
test_macro.c:9:4: erreur: ‘BB’ undeclared (first use in this function)
test_macro.c:9:4: note: each undeclared identifier is reported only once for each function it appears in
最佳答案
您可能想要通过扩展AA(n,k)
来提供BB
的附加参数。正如 Sourav Ghosh 所指出的,在您的程序中,AA(n,k)
在作为单个参数传递给 BB
后被展开。要在之前对其进行扩展,您可以使用更多的宏级别并将您的程序定义为:
#define AA(a1, a2) a1, 3, 5, a2
#define BB(x, y1, y2, y3, y4) { printf("%d %d %d %d %d\n",x, y1, y2, y3, y4 ); }
#define BBB(a,b) BB(a,b)
int main ()
{
int n = 21, k= 11;
BBB(31, AA(n,k));
}
关于c - 如何在宏中调用宏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31293714/