1 1 vote closed as a duplicate of: solve it #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; } Programming in C programming-in-c non-gatecse + – Ashwani Kumar 2 2.4k views comment Share Follow Print See all 4 Comments 4 4 Comments reply srestha commented Jun 25, 2017 reply Follow flag giving output 513 even if printing x, it is giving a large output not getting how. @Kapil @Debashish can u plz chk it? 0 0 replyShare Tauhin Gangwar commented Jun 25, 2017 reply Follow flag Answer is correct srestha..wats the problem 0 0 replyShare srestha commented Jun 28, 2017 reply Follow flag @Tauhin can u explain it more clearly? 0 0 replyShare Tauhin Gangwar commented Jun 30, 2017 reply Follow flag Srestha check it 0 0 replyShare Please log in or register to add a comment.
0 0 votes Output is 513 in a little endian machine. To understand this output, let integers be stored using 16bits. In a little endian machine, when we do x[0] = 1 and x[1] = 2, then umber a is changed to 00000001 00000010 which is representation of 513 in a little endian machine. alokraj1142 answered Jun 25, 2017 alokraj1142 comment Share Follow 0 reply Please log in or register to add a comment.
0 0 votes #include <stdio.h> int main() { int a; char *x; x= (char *) &a;// here type casting as character pointer point only to character variable a=512;//00000000(at x[0]) 00000001(at x[1]) as little endian x[0]=1;// 00000001 00000001 x[1]=2;// 00000001 00000010 printf("%d\n",a); ////513 return 0; } akankshadewangan24 answered Jun 28, 2017 akankshadewangan24 comment Share Follow See all 12 Comments 12 12 Comments reply Show 9 previous comments srestha commented Jun 28, 2017 reply Follow flag visited the link There are so many problems in this question, first tell me a=512 Now x[0]=1 x[1]=2 So, x[]=21 Now, we are printing a a must be same , So, why not it printing 512? 0 0 replyShare srestha commented Jun 28, 2017 reply Follow flag Am I wrong somehow? 0 0 replyShare akankshadewangan24 commented Jun 28, 2017 reply Follow flag char *x; x= (char *) &a; here take a look is an integer variable so now it will be pointed by character pointer thats why type casting is done as not character is of 1 byte . now, a= 512 = 00000001 00000000 so as per little endian the MSB will filled in LSB side and vice versa so a= 00000000 00000001 now X[0]=1 , means as X is a character array so for this MSB will be X[0] therefore new a= 00000001 00000001 now X[1]=2 , means as X is a character array so for this right from MSB will be X[1] therefore new a = 00000001 00000011 now as a execution of %d come which take a as a whole 2 byte so for this it will take MSB first and first and as u know in integer it will be 16 bit there for it conscider whole 16 bit print a=(00000001 0000001{x[1]}1{x[0]}) = 513 hope it will be helpful for u 0 0 replyShare Please log in or register to add a comment.