Chris said:
It's not significantly more effort to write an inline function than
it is to write the corresponding function macro.
And that function will have type checking and also be immune to evil
macro side effects.
Using macros for functions is both lazy and stupid. Of course, I have
admitted that I do it from time to time {what does that say about
me...}
(It's even less effort to write a non-inline function for the same
thing.)
Which most compilers will inline if you ask them to (and in a way that
is smarter than you, for the most part).
/* Some people might even be surprised by the output of this: */
#define square(x) ((x)*(x))
#define even(x) ((((x)>>1)<<1) == (x))
#include <stdio.h>
int main(void)
{
long long ll = 217;
long l = 19;
int i = 2;
char c = 8;
if (even(square(++c)))
puts("square of 8+1 is even");
else
puts("square of 8+1 is odd");
if (even(square(++i)))
puts("square of 2+1 is even");
else
puts("square of 2+1 is odd");
if (even(square(++l)))
puts("square of 19+1 is even");
else
puts("square of 19+1 is odd");
if (even(square(++ll)))
puts("square of 217+1 is even");
else
puts("square of 217+1 is odd");
ll = 217;
l = 19;
i = 2;
c = 8;
printf("218 squared = %.0f\n", (double) square(++ll));
printf("20 squared = %.0f\n", (double) square(++l));
printf("3 squared = %.0f\n", (double) square(++i));
printf("9 squared = %.0f\n", (double) square(++c));
ll = 217;
l = 19;
i = 2;
c = 8;
if (even(++ll))
puts("218 is even");
else
puts("218 is odd");
if (even(++ll))
puts("20 is even");
else
puts("20 is odd");
if (even(++ll))
puts("3 is even");
else
puts("3 is odd");
if (even(++ll))
puts("9 is even");
else
puts("9 is odd");
return 0;
}
/*
dcorbit@DCORBIT64 /c/tmp
$ gcc -Wall -ansi -pedantic t.c
t.c: In function 'main':
t.c:7: warning: ISO C90 does not support 'long long'
t.c:37: warning: operation on 'll' may be undefined
t.c:38: warning: operation on 'l' may be undefined
t.c:39: warning: operation on 'i' may be undefined
t.c:40: warning: operation on 'c' may be undefined
dcorbit@DCORBIT64 /c/tmp
$ ./a
square of 8+1 is odd
square of 2+1 is odd
square of 19+1 is odd
square of 217+1 is odd
218 squared = 47961
20 squared = 441
3 squared = 16
9 squared = 90
218 is odd
20 is odd
3 is odd
9 is odd
dcorbit@DCORBIT64 /c/tmp
$
*/
Of course, nasal demons are another possible output.