我试图通过定义调用正确代码的操作类型来使用#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...如果我用数字替换操作它工作正常....但我希望它工作,如上所述.
任何帮助