0

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.

Vertinhol
  • 289
  • 1
  • 8
  • 3
    The preprocessor does not do this, and that is not what it is designed for. In spite of your comment about “another electronic project in which I have to use words instead of numbers”, you do not show any reason that using numbers would be a problem. – Eric Postpischil Jan 02 '22 at 11:30
  • In C you cannot directly compare strings. With the code you have characters or integers will work just fine. Possible duplicate of [this](https://stackoverflow.com/q/2335888/4549234). – Devansh Sharma Jan 02 '22 at 11:32
  • `Is there a way to achieve something like this in C please?` Yes you `can make it work by changing "sum" and "dif" with numbers`. (?) – KamilCuk Jan 02 '22 at 11:32
  • What is an electronic project and how does it affect this task? – n. m. could be an AI Jan 02 '22 at 12:07
  • Thank you for your comments, so imagine you have to make a project that works with multiple microcontrollers, or the same project can be used to do different things, for example if you change the trigger mode of a simple counter then the numbers could increment or decrement with the same code. – Vertinhol Jan 02 '22 at 12:48

1 Answers1

0

You cannot use sum and dif like symbols in this way, you need to define them as numbers:

#include <stdio.h>

#define sum 0
#define dif 1
#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(void)
{
    printf("%d\n",function(5,3));

    return 0;
}
August Karlstrom
  • 10,773
  • 7
  • 38
  • 60
  • The question asks for a way to do it without using numbers. So supplying a solution with numbers is not a correct answer. – Eric Postpischil Jan 02 '22 at 11:42
  • That's just an example, imagine you have multiple functions, and every function could have many possible cases, so can you imagine how difficult it will be using numbers? – Vertinhol Jan 02 '22 at 12:52
  • @Vertinhol normally that is not difficult at all. At some place you must decide what values are allowed to compare with. Then just add a `#define` for each of those values. To avoid duplicate numbers, you may put all this defined into one place. The way it is done at the top of this snippet and also at the top of your second snippet in your question is clean and I have no idea how this would become "difficult". That also provides a list of all allowed values you can chose from when defining your `mode` macro. – Gerhardh Jan 02 '22 at 13:22