0
Can anyone please explain the answer of the below mentioned question?
#include<stdio.h> #define square(x) x*x int main() { printf("%d", square (4-1)); return 0; } The answer mentioned was -1
2 Antworten
+ 1
#define is a macro, it will just replace square(expression) by expression*expression. so
x=square(4-1)= 4 - 1*4 - 1= 4 - 4 - 1 = -1
if you want it to really calculate the square, use:
#define square(x) (x)*(x)
look here
https://www.sololearn.com/Discuss/2233917/?ref=app
+ 1
in your printf function...the square(4-1) is replaced with 4-1 * 4-1...so
4 - 1 * 4 - 1...
4 (-1 * 4) - 1..
4 - 4 - 1...
result = -1