-1

I am getting the following warning:

warning: left-hand operand of comma expression has no effect

The macros are defined below. I am compiling with GCC (4.4.3) on Linux. It is C code.

#define MY_MAX(a,b) \
   ({ __typeof__ (a) _a = (a); \
       __typeof__ (b) _b = (b); \
     _a > _b ? _a : _b; })

#define MY_MIN(a,b) \
   ({ __typeof__ (a) _a = (a); \
       __typeof__ (b) _b = (b); \
     _a < _b ? _a : _b; })

How do I fix them to get rid of the warnings?

[[Update]]

Actually, I found the cause of the warning. It had nothing to do with the macro itself. It was because I was trying to find the min of two numbers, one of which was a #def named as MAXIMUM_ARRAYSIZE. It was defined as:

#define MAXIMUM_ARRAYSIZE (sizeof(size_t)==2,16384,1073741824)

That is a pretty strange macro definition.

Homunculus Reticulli
  • 65,167
  • 81
  • 216
  • 341
  • 2
    Could you show the call site where the warning is triggered? (Tagged GCC, the macros use a GCC extension.) – Mat Jan 07 '12 at 15:09
  • Does [this](http://stackoverflow.com/questions/3437404/min-and-max-in-c) help? – César Jan 07 '12 at 15:12
  • I don't see a single comma in the code here. So I guess the problem is in the code that uses the macro (which probably contains a comma), not the macro itself. – ugoren Jan 07 '12 at 15:23
  • What arguments are you using to produce the warning? – Brent Worden Jan 07 '12 at 15:23

3 Answers3

1

The error is not in your macro definition.

You'll need to show us the calling code to the MIN/MAX macros. It sounds like you're either trying to use the result of MIN/MAX as incorrectly, or you've passed a token (a or b) containing an unexpected comma.

Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
0

I tried the following and it worked fine without any warnings/error with gcc -Wall. Check how you're using those macros!

#include <stdio.h>

#define MY_MAX(a,b) \
   ({ __typeof__ (a) _a = (a); \
       __typeof__ (b) _b = (b); \
     _a > _b ? _a : _b; })

#define MY_MIN(a,b) \
   ({ __typeof__ (a) _a = (a); \
       __typeof__ (b) _b = (b); \
     _a < _b ? _a : _b; })

int main(void)
{
    printf("%d \n", MY_MAX(10,20));
    printf("%d \n", MY_MIN(10,20));
    return 0;
}

Output:

$ gcc ma.c -Wall
$ ./a.out 
20 
10 
$ 
Sangeeth Saravanaraj
  • 16,027
  • 21
  • 69
  • 98
-2

_a < _b ? _a : _b is an expression, not a statement, but you have written it as a statement (all by itself followed by a semicolon).

ajd
  • 1,022
  • 1
  • 8
  • 21
  • 3
    `({...})` is a GCC extension that *is* an expression (which evaluated to the result of the last "statement"). If it wasn't, you'd get a syntax error instead. And if that was somehow averted, you'd probably get a different warning (e.g. "statement has no effect") - there's no comma operator in the macro. –  Jan 07 '12 at 15:16