Skip to content
Related Articles

Related Articles

Improve Article

GATE | GATE MOCK 2017 | Question 44

  • Difficulty Level : Hard
  • Last Updated : 28 Dec, 2020

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: The binary equivalent of 300 is 00000001 00101100. Since the machine is little endian, 00000001 00101100 is stored as 00101100 00000001 in the memory in the form of an array.

Pointer b is then given the address of a. It means that the address of first byte of 00101100 00000001 is allotted to b (hence b is allotted 00101100).

*++b = 2 increments the value of b by 1 so that b now points to the next memory address which is 00000001 and the binary equivalent of 2 (00000010) will be stored here.

Finally, a becomes 00101100 00000010, which in decimal is basically 556.



Please read the following link :

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

Quiz of this Question

Attention reader! Don’t stop learning now.  Practice GATE exam well before the actual exam with the subject-wise and overall quizzes available in GATE Test Series Course.

Learn all GATE CS concepts with Free Live Classes on our youtube channel.

My Personal Notes arrow_drop_up
Recommended Articles
Page :