You can’t use this macro outside of a function, because it’s an arbitrary expression, that’s why you’re getting an error.
Just move the invocation of the macro into function scope and it will work:
#define MAX(x, y, r) ((x) > (y) ? (r = x) : (r = y))
int x = 10;
int y = 20;
int r;
int main()
{
MAX(x, y, r);
}
Using macros in this case is, however, unnecessary (unless this is just an exercise to learn macro usage); making max
a function (or, better yet, using std::max
) would be a better and less error-prone way.
5
solved Macros in C++ breaks with postfix increments