Consider the macro replacement using this macro:
#define SQR(x) (x*x)
Using b+5
as the argument. Do the replacement yourself. In your code, SQR(b+5)
will become: (b+5*b+5)
, or (3+5*3+5)
. Now remember your operator precedence rules: *
before +
. So this is evaluated as: (3+15+5)
, or 23
.
The second version of the macro:
#define SQR(x) ((x) * (x))
Is correct, because you're using the parens to sheild your macro arguments from the effects of operator precedence.
This page explaining operator preference for C has a nice chart. Here's the relevant section of the C11 reference document.
The thing to remember here is that you should get in the habit of always shielding any arguments in your macros, using parens.