+ 1
Why this code returns 3?
5 Antworten
+ 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.
+ 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....
+ 2
Jayakrishna🇮🇳 thanks I got it...
+ 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.
+ 1
Brian thanks, now it's more clear to me...