【问题标题】:Is it possible to call the function from the C macro definition in C [duplicate]是否可以从 C 中的 C 宏定义中调用该函数 [重复]
【发布时间】:2021-03-28 06:32:50
【问题描述】:
#include <stdio.h>

#define ADD_NUMMBERS numbers_add() /*this is called in the below Marco*/

#define ADDED_NUMBERS (a, b, c)  \
{                                  \
  if(ADD_NUMMBERS)  \
    add_afb(a, b, &c); \
  else                                 \
    add_bfa(a, b, &c); \
}

int numbers_add (int a, int b)
{
    if (a > b)
      return 1;
    else 
      return 0;
}

int add_afb (int a, int b, int *c)
{
    *c = a+b+10;
}

int add_bfa (int a, int b, int *c)
{
    *c = a+b+20;
}


int main()
{
    int a, b, c; 
    printf("\n enter the a and b values");
    scanf("\n %d %d", &a, &b);
    
    ADDED_NUMBERS (a, b, c);  
    return 0;
}

main.c:在函数'main'中:main.c:14:1:错误:预期';'之前 '{' 令牌 { \ ^ main.c:46:5: 注意: 在宏“ADDED_NUMBERS”的扩展中 ADDED_NUMBERS (a, b, c);

我收到上述错误,但无法找出问题所在。 请任何帮助表示赞赏。如果不可能,也建议任何替代方案。

【问题讨论】:

  • 您可以在预处理后停止编译器,并查看宏 ADDED_NUMBERS 在您的代码中扩展为什么。对于 GCC,这是通过 -E 选项完成的。
  • 你没有向numbers_add()传递任何参数
  • 提示:宏定义中的空格非常重要。宏定义 #define ADDED_NUMBERS (a, b, c) 并没有按照您的预期进行。

标签: c compiler-errors macros user-defined-functions


【解决方案1】:

尝试为函数#define ADDED_NUMBERS (a, b, c) 删除空间

#define ADDED_NUMBERS(a, b, c) \
{                               \
  if(ADD_NUMMBERS(a, b))        \
    add_afb(a, b, &c);          \
  else                          \
    add_bfa(a, b, &c);          \
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-15
    • 2016-02-24
    • 1970-01-01
    • 1970-01-01
    • 2013-09-13
    • 2015-10-16
    • 1970-01-01
    • 2015-09-04
    相关资源
    最近更新 更多