0
#include<stdio.h>
#define SQR(x) (x*x)
int main(){
int a;
a= SQR(3-4);
printf("%d",a);
return 0;
} 

Output :-13

How does the macro function work here to give the output as -13?

Brian Tompsett - 汤莱恩
  • 5,438
  • 68
  • 55
  • 126
user3168680
  • 35
  • 1
  • 8

1 Answers1

1

The expression inside SQR gets 3-4*3-4, and given the precendence of operators, gives you that result. This is a common mistake in macros. In principle, every argument should be surrounded with parentheses, if it involves some calculation:

#define SQR(x) ((x)*(x))

You'll get the expected result.

Diego Sevilla
  • 27,697
  • 3
  • 55
  • 86