84 views
int main()
{
int a = 300;
char *b = (char *)&a;
*++b = 2;
printf("%d ",a);
return 0;
}



Consider the size of int as two bytes and size of char as one byte.Machine is little-endian

O/P_________

a=300-->(00000001  00101100)2

now b will point to 00101100

 *++b = 2; what will this do ?

300 = 00000001 00101100

endian machine reads in backward direction.

hence b -> 00101100

*++b will be = incrementation of b and then pointing to that value and change that value to 2.

that means after incrementing pointer b by 1, it will point to next 8 bits (i.e. b will point to 00000001)

and *(++b) = 2 will change that value to 2 (i.e. changed value is 00000010)

and hence final value of a = 00000010 00101100

Its decimal equivalent is (22 + 23 + 25 + 29) = 556.

Hence ans is 556

selected
0

Thank You once again :)

0
You're welcome! You've great collection of these type of problems! :)
0
haha No I was going through random problems over internet for practice .. :)
+4

endian machine reads in backward direction.

this is true only for little endian m/c as it stores lower byte at lower address and higher byte at higher address unlike in big endian