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 srestha commented Jun 28, 2017 reply Follow flag 512 in little endian will be 1205 rt? how r u getting 00000000(at x[0]) 00000001(at x[1]) 0 0 replyShare akankshadewangan24 commented Jun 28, 2017 reply Follow flag here integer is printng which wil take 2 byte so it will take whole 2 byte to print thats why 513 0 0 replyShare srestha commented Jun 28, 2017 reply Follow flag Cannot understand, can u elaborate. what is 8 bit representation of 1 or 3? 0 0 replyShare akankshadewangan24 commented Jun 28, 2017 reply Follow flag 1 === 00000001 3 === 00000011 in little endian if 31 ==== 00000001 00000011 0 0 replyShare srestha commented Jun 28, 2017 reply Follow flag but u have written in 16 bit representation.how getting 16 bit x[0]=1;// 00000001 00000001 x[1]=2;// 00000001 00000010 0 0 replyShare akankshadewangan24 commented Jun 28, 2017 reply Follow flag 8- 8 bit pointed by character pointer which is divide from integer to character while type casting these is my concept 0 0 replyShare srestha commented Jun 28, 2017 reply Follow flag can u give me some link? still not getting. do u added bitwise x[1],x[2]? 0 0 replyShare akankshadewangan24 commented Jun 28, 2017 reply Follow flag what is ur doubt i can't understand? 0 0 replyShare akankshadewangan24 commented Jun 28, 2017 reply Follow flag http://cs-fundamentals.com/tech-interview/c/c-program-to-check-little-and-big-endian-architecture.php visit this 0 0 replyShare 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.