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,753
  • 72
  • 57
  • 129
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
  • 28,636
  • 4
  • 59
  • 87