endian
closed

closed by
941 views
2 votes
2 votes
closed as a duplicate of: Programming and DS
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 ?
closed by

1 Answer

Best answer
4 votes
4 votes

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 by

Related questions

0 votes
0 votes
2 answers
1
Narayan Kunal asked Sep 4, 2014
759 views
I saw on http://www.cquestions.com/2009/06/memory-representation-of-int-data-type.html but it seems wrong to me. Please clarify.
1 votes
1 votes
1 answer
3
Pawan Kumar 2 asked Nov 23, 2017
307 views
how this hex representation is done?