0
I don't understand a challenge answer in C
Here is the code of the question. I tried it with C tutor but unfortunately, it didn't help me to understand : #define prod(i, j) i*j int main() { int x = 3; int y = 4; printf("%d", prod(x+2,y-1)); return 0; } I don't understand why the answer isn't 15 since : x + 2 = 5 y - 1 = 3 5 * 3 = 15 Can someone explain this ?
3 ответов
+ 1
So it was only about the brackets.
Thank you :)
+ 1
[BusyTheseDays] I agree with your answer (although your first example has a mistake in the y substitution). Let me add to your answer and show that it is necessary to surround the whole macro with parentheses as well as the parameters. Else, division gives a wrong result:
x=3, y=4
15/prod(x+2,y-1) //15/15 = 1, yes?
15/(x+2)*(y-1) //!!!
15/(3+2)*(4-1)
15/5*3
3*3
9 /* Expected 1 :-( */
Final macro fix:
#define prod(i,j) ((i)*(j))