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)

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 :

Quiz of this Question

My Personal Notes arrow_drop_up

Article Tags :

Be the First to upvote.

Please write to us at to report any issue with the above content.