The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
0 votes
49 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 ?
asked in Programming by Active (1.8k points) | 49 views

1 Answer

+3 votes
Best answer

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

answered by Boss (5.4k points)
selected by

Thank You once again :)

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

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

Related questions

0 votes
2 answers
1
+1 vote
1 answer
2
+2 votes
0 answers
3


Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

29,997 questions
37,681 answers
96,743 comments
35,329 users