retagged by
613 views
5 votes
5 votes
#include <stdio.h>
int main()
{
    int i= 255;
    short int *s= (short int *)&i;
    printf("%d\n", *s);
}


What will be the output of the above program in little-endian and big-endian, respectively?

$(65280\text{ is } 255\times2^8)$

  1. $255,\; 0$
  2. $65280,\; 0$
  3. $0,\;0$
  4. $0,\; 65280$
retagged by

1 Answer

4 votes
4 votes

$i$ = 255, since int is $4B$

 which in binary is $0\text{x}11111111$

short int *s = (short int *) &i; Since short is 2B it will point to only $16$ bits.

If it is stored in Little-endian then least significant byte is stored in the lowest address. s will point to initial $2B$ that is$11111111$ $00000000$ = $255$

If it is stored in Big-endian then most significant byte is stored in the lowest address. s will point to $2B$ that is $00000000$ $00000000$ = $0$

Answer:

Related questions