Why this code returns 3? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
14th Oct 2021, 1:57 PM
Roshan Diwan
Roshan Diwan - avatar
5 Answers
+ 2
When the macro (x*x) gets expanded it simply replaces x with y+1. See what happens when it does that: (x*x) becomes (y+1*y+1) Now do the math.
14th Oct 2021, 2:02 PM
Brian
Brian - avatar
+ 2
#define square(x) (x*x) int main() { int x, y=1; x=square(y+1); /* here square(y+1) is just replaced as (y+1*y+1) and then evaluated as x = y+1*y+1 =1+1*1+1 =1+1+1 = 3 so x=3 if you define square (x) as ((x)*(x)) then output you get as 4 */ printf("%d\n",x); return 0; } //hope it helps....
14th Oct 2021, 2:06 PM
Jayakrishna 🇮🇳
+ 2
Jayakrishna🇮🇳 thanks I got it...
14th Oct 2021, 2:09 PM
Roshan Diwan
Roshan Diwan - avatar
+ 1
Roshan Diwan check again, please. If y=1: (y+1*y+1) becomes (1+1*1+1) It reduces to (1+1+1) = 3.
14th Oct 2021, 2:09 PM
Brian
Brian - avatar
+ 1
Brian thanks, now it's more clear to me...
14th Oct 2021, 2:12 PM
Roshan Diwan
Roshan Diwan - avatar