The lines
b1 = SQ(a1++);
b2 = SQ(++a2);
expand to
b1 = a1++ * a1++;
b2 = ++a2 * ++a2;
which invoke undefined behavior (an object may have its value modified at most once between sequence points), meaning any result is considered "correct".
That's the problem with these sorts of macros; you really want the argument to be evaluated once, but because of the expansion it gets evaluated twice.
There's also the problem with calling it with an expression like
x = SQ(a + b);
This will expand to
x = a + b * a + b;
which is probably not what you wanted. To preserve operator precedence, the expansion should be wrapped in ()
, such as
#define SQ(x) (x) * (x)