-4

Want to know how "11" is the answer of this c preprocessor instruction:

#define SQR(x) x*x

int main()
{
    SQR(2+3);
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

1 Answers1

0

Try expanding the macro manually.

It will be 2+3*2+3 and this is evaluated as 11.

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • 1
    Why downvoted your answer? it is correct. Maybe a little ... minimal, but correct. Better add that this is the reason why is always better to add parenthesis around parameters. Try this: `#define SQR(x) ((x)*(x))` – Frankie_C Feb 28 '16 at 14:33
  • im just wondering why it does 2+3*2+3 – vamosfener Feb 28 '16 at 14:43
  • @vamos Order of operations: multiplication first, then addition from left to right. 3 * 2 is 6, plus 2 is 8, plus 3 is 11. – Cody Gray - on strike Feb 28 '16 at 14:54