小编A.S*_*SDR的帖子

使用枚举的C宏

我试图通过定义调用正确代码的操作类型来使用#if宏,所以我做了一个非常简单的例子,类似于我想要做的事情:

#include <stdio.h>

enum{ADD,SUB,MUL};

#define operation   ADD

int main()
{
   int a = 4;
   int b = 2;
   int c;

   #if (operation == ADD)
      c = a+b;
   #endif

   #if (operation == SUB)
      c = a-b;
   #endif

   #if (operation == MUL)
      c = a*b;
   #endif


   printf("result = %i",c);
   return 0;
}
Run Code Online (Sandbox Code Playgroud)

但不幸的是,这不起作用我得到以下result = 8...如果我用数字替换操作它工作正常....但我希望它工作,如上所述.

任何帮助

c macros enums

2
推荐指数
2
解决办法
707
查看次数

标签 统计

c ×1

enums ×1

macros ×1