#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?
#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?
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.