Skip to content
Related Articles

Related Articles

C | Pointer Basics | Question 7
  • Difficulty Level : Hard
  • Last Updated : 04 Feb, 2013




#include<stdio.h> 
int main() 
   int a; 
   char *x; 
   x = (char *) &a; 
   a = 512; 
   x[0] = 1; 
   x[1] = 2; 
   printf("%d\n",a);   
   return 0; 
}


What is the output of above program?
(A) Machine dependent
(B) 513
(C) 258
(D) Compiler Error


Answer: (A)

Explanation: Output is 513 in a little endian machine. To understand this output, let integers be stored using 16 bits. In a little endian machine, when we do x[0] = 1 and x[1] = 2, the number a is changed to 00000001 00000010 which is representation of 513 in a little endian machine.

Attention reader! Don’t stop learning now. Get hold of all the important C++ Foundation and STL concepts with the C++ Foundation and STL courses at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :