-2

how does one define a macro expression in C, whose arguments are also expressions?

The problem I am tackling is how to write an expression MAX(X,Y) that takes in the expressions X and Y (such as x++ or x+2) and returns the maximum of the two. This expression should be defined in as a preprocessor macro.

Answer from a related post:

#define MAX(a, b) ((a) > (b) ? (a) : (b))

and I am aware of the risks involved with x++ in a macro (can be evaluated twice).

Kind regards

  • 2
    Expressions like `x+2` which have no side effects are fine to use in a macro. But an expression like `x++` should not be passed to a macro, unless you know for a fact that the expression will only be evaluated once. Which is why you're better off just writing a `max` function and let the compiler inline it. – user3386109 Aug 31 '22 at 07:15
  • [What are expressions with side effects and why should they be not passed to a macro?](https://stackoverflow.com/questions/32284073/what-are-expressions-with-side-effects-and-why-should-they-be-not-passed-to-a-ma) – tshiono Aug 31 '22 at 07:18
  • I am aware of the issues with macros and why they should be avoided, it's just a specific practice problem that involves macro expressions that I came along. Thanks for the link, the solutions there answers my question :D –  Aug 31 '22 at 07:23
  • Cannot be done... Even if your macro redirects to a function, `x` & `y` may not match the datatypes of the function... `char` is one byte, and `long long` quite a few more... Even `memcmp()` won't work without knowing the size of the datatypes involved.. Heaven help you if you want to determine MAX( 42, 37.53 )... – Fe2O3 Aug 31 '22 at 07:24
  • With pure standard C you could write it as `#define MAX(a,b) (parse(#a) > parse(#b) ? (a) : (b) )`. Incredibly inefficient but only executes each expression once. – Lundin Aug 31 '22 at 07:32
  • 1
    What's `parse`? – Armali Aug 31 '22 at 08:16
  • @Armali Some custom expression parser which you'd have to write yourself... as I said, incredibly inefficient. The correct solution is to write a function. – Lundin Aug 31 '22 at 09:09
  • 1
    Many macro problems can be solved by using a function instead. A function call will only evaluate the parameters once. – BoP Aug 31 '22 at 09:17

1 Answers1

1

This is possible with GCC by using a specific expression construct, see: Statements and Declarations in Expressions.

Here is an example:

#define INTMAX(A, B) ({ int _a = (A), _b = (B) ; _a > _b ? _a : _b ; })

You can use typeof instead of int if you don't know the type of arguments.

mouviciel
  • 66,855
  • 13
  • 106
  • 140
  • There's no indication of the OP using gcc, so this answer is kind of off-topic. – Lundin Aug 31 '22 at 07:27
  • 2
    @Lundin There's no indication of the OP sticking with another compiler despite gcc solving their problem. – mouviciel Aug 31 '22 at 07:31
  • SO doesn't work like that. You can't go post an answer in C++ either, unless the OP specifically mentioned using it. Also as it happens, excessive use of gcc non-standard extensions has turned into a pox and shouldn't be encouraged. – Lundin Aug 31 '22 at 07:35