I would like to define a variable (Mode) to choose between two functions, but the compiler doesn't seem to do what I want, here is the code:
#include <stdio.h>
#define Mode dif
#if Mode == sum
int function(int a, int b)
{
return a+b;
}
#elif Mode == dif
int function(int a, int b)
{
return a-b;
}
#else
ERROR
#endif
int main()
{
printf("%d",function(5,3));
return 0;
}
The compiler chooses the function "sum" (or the first one I wrote) for any string value (or an integer 0) I put in "Mode", and it chooses the "ERROR" state for any integer I put besides 0, that's what I observed so far.
I can make it work by changing "sum" and "dif" with numbers but I'm working on another electronic project in which I have to use words instead of numbers otherwise it will be difficult to implement and diagnose, for examlpe:
#define Falling_Edge 0x00
#define Rising_Edge 0x01
#define Both 0x02
#define IT_Mode X // X could be Falling_Edge, Rising_Edge or Both
#if IT_Mode == Falling_Edge
// do something
#elif IT_Mode == Rising_Edge
// do something
#elif IT_Mode == Both
// do something
#elif
ERROR
#endif
Is there a way to achieve something like this in C please?
Thank you.