I don't understand a challenge answer in C | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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 ?

17th Oct 2021, 1:41 PM
Mélodie Bernard
Mélodie Bernard - avatar
2 Answers
+ 1
So it was only about the brackets. Thank you :)
17th Oct 2021, 2:02 PM
Mélodie Bernard
Mélodie Bernard - avatar
+ 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))
17th Oct 2021, 2:56 PM
Brian
Brian - avatar