0

Give explanation to this question

#define square(x) x*x Printf("%d",square(4+1));

20th Dec 2018, 9:10 AM
Rajasekhar Reddy
Rajasekhar Reddy - avatar
3 Answers
+ 6
Notice that wrapping parenthesis may eliminate the problem but it wouldn't be a remedy for undefined behavior caused by increment/decrement operator like so int a = 4; printf("%d", square(++a)); // UB _____ https://en.wikipedia.org/wiki/Undefined_behavior#Examples_in_C_and_C++
20th Dec 2018, 9:37 AM
Babak
Babak - avatar
+ 2
You're defining a macro/preprocessor directive that will replace every occurrence of "square(x)" in the source code with "x*x" before the code is compiled. Unfortunately, this will lead to wrong/unexpected results because it will replace "square(4+1)" with "4+1*4+1", which is 9, instead of 25. To avoid that, you can use #define square(x) (x)*(x) instead: square(4+1) => (4+1)*(4+1) = 25.
20th Dec 2018, 9:17 AM
Anna
Anna - avatar
0
Answer 25 or 9
20th Dec 2018, 9:22 AM
Rajasekhar Reddy
Rajasekhar Reddy - avatar