0
Why result is 8 but not 2?
#include<stdio.h> #define sq(x) x*x int main() { int i = 8/sq(2); printf("%d", i); }
3 Respostas
+ 6
Macro call is not a function call but a textual replacement before compilation. If you replace sq(2) in the expression, you get
int i = 8/2*2
Since multiplication and division have the same precedence, it is evaluated left to right. That is, (8/2)*2, and that is 8.
+ 5
Jay Matthews better yet, this would produce the expected results:
#define sq(x) ((x)*(x))
+ 1
It is because same precedence of * & / , and left to right evaluation.