#include <stdio.h>
void f();
int main()
{
long int i;
i=20;
f();
i = 10;
printf ("\n%d\n",i) ;
return 0;
}
void f()
{
int j=20;
*(&j+2)+=7;
}
I found this question in the internet. The output is 20. Even I didn’t
believe this till I executed this program.
The probable explanation will be:
&j refers to the address of j.
&j + 2 refers to the address next to j(since j is of type <int).
This may be place where the return address is stored.(i think the
return address is stored immediately after the variables).
That value is incremented by 7.
May be the assignment statement takes 7 bytes to be represented in the
macine language.
Therefore the assignment statement in the main(),<i=10wil l be
skipped.
So the value of "i" is never altered.
Is my explanation correct..??
void f();
int main()
{
long int i;
i=20;
f();
i = 10;
printf ("\n%d\n",i) ;
return 0;
}
void f()
{
int j=20;
*(&j+2)+=7;
}
I found this question in the internet. The output is 20. Even I didn’t
believe this till I executed this program.
The probable explanation will be:
&j refers to the address of j.
&j + 2 refers to the address next to j(since j is of type <int).
This may be place where the return address is stored.(i think the
return address is stored immediately after the variables).
That value is incremented by 7.
May be the assignment statement takes 7 bytes to be represented in the
macine language.
Therefore the assignment statement in the main(),<i=10wil l be
skipped.
So the value of "i" is never altered.
Is my explanation correct..??
Comment