Nick is right, try:
int main(int argc, char **argv)
{
unsigned int x = 7, y = 5;
printf("%d\n", avg(x,y));
return 0;
}
It fails because 5-7 = -2, which needs a signed division or sign
extending right shift.
we'd need something like:
#define avg(x, y) ({ \
typeof(x) _avg1 = (x); \
typeof(y) _avg2 = (y); \
(void) (&_avg1 == &_avg2); \
_avg1 + (signed typeof(x))(_avg2 - _avg1)/2; })
except that typeof() doesn't work that way.
#define avg(x, y) ({ \
typeof(x) _avg1 = (x); \
typeof(y) _avg2 = (y); \
(void) (&_avg1 == &_avg2); \
_avg1 + (long)(_avg2 - _avg1)/2; })
works for the above example, but when I make it long long, so as to
match the longest supported type, it goes boom again - for as of yet
unknown reasons.
--