c - 如何在宏中调用宏?

标签 c macros

是否可以通过这种方式在宏内部调用宏:

#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/

相关文章:

c++ - 使用数组时的常见陷阱 : Trusting type-unsafe linking

计算数组中的音节

c - 使用变量 args 声明函数指针

c - C 中的宏和函数冲突

http - std::io::TcpStream::read_as_string 返回空字符串

c - 在 C 中使用用户输入的字符串从一个函数到另一个函数

python - Cython函数内存泄漏

c - 整数到带有宏的罗马数字

c - 为什么 avr-gcc 在扩展我的 [fancy] 宏时告诉我缺少参数?

c - (C99)在不同的宏中扩展一个宏