endian
closed

• closed by
1,631 views
2 2 votes
closed as a duplicate of: Programming and DS
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.Machine is little-endian

O/P_________

a=300-->(00000001  00101100)2

now b will point to 00101100

 *++b = 2; what will this do ?

1 Answer

Best answer
4 4 votes

300 = 00000001 00101100

endian machine reads in backward direction.

hence b -> 00101100

*++b will be = incrementation of b and then pointing to that value and change that value to 2.

that means after incrementing pointer b by 1, it will point to next 8 bits (i.e. b will point to 00000001)

and *(++b) = 2 will change that value to 2 (i.e. changed value is 00000010)

and hence final value of a = 00000010 00101100

Its decimal equivalent is (22 + 23 + 25 + 29) = 556.

Hence ans is 556

• selected by
Position:
Show:

Related questions

0 0 votes
3 3 answers
1.6k
1.6k views
Narayan Kunal asked Sep 4, 2014
1,598 views
I saw on http://www.cquestions.com/2009/06/memory-representation-of-int-data-type.html but it seems wrong to me. Please clarify.
6 6 votes
2 answers 2 answers
319
319 views
GO Classes asked Aug 22, 2025
319 views
If the code shown below is executed on a big-endian machine, then the output of the following C code will be ?(Assume int takes 4 bytes and char takes 1 byte in memory.) ...
–1 –1 vote
0 0 answers
685
685 views
KUSHAGRA गुप्ता asked Jun 9, 2019
685 views
#include <stdio.h>int main(){ unsigned char arr = {0x01, 0x00}; unsigned short int x = *(unsigned short int *) arr; printf("%d", x); getchar(); return 0;}...
1 1 vote
1 answers 1 answer
630
630 views
Pawan Kumar 2 asked Nov 23, 2017
630 views
how this hex representation is done?