GATE | GATE MOCK 2017 | Question 44

Consider the following C code

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. Predict the output of the following code .
Assume that the machine is little-endian.
(A) 556
(B) 300
(C) Runtime Error
(D) Compile Time Error


Answer: (A)

Explanation:
Ans is 556 as char pointer will change the second byte of the integer in the memory and when you print it as a whole integer using %d , its value will be 556 considering little endian scenario.
Please read the following link :

http://stackoverflow.com/questions/22030657/little-endian-vs-big-endian

Quiz of this Question



My Personal Notes arrow_drop_up


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.