retagged by
584 views
3 votes
3 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

2 Answers

5 votes
5 votes

Answer: A

Big Endian: Table of address and data in hex

int i
1000 1004 1008 1012
00 00 00 FF

 

s points to address 1000. Since s is a short pointer it will read data from 1000 – 1007 which is all zeroes: 0

Little Endian: Table of address and data in hex

int i
1000 1004 1008 1012
FF 00 00 00

In this scenario also s points to the address 1000. In little endian bytes are stored in reverse but those are also read in reverse.

s reads the data from 1000 – 1007 which is oxFF 00. It is first reversed 0x00 FF and then interpreted.: 255

 

Answer:

Related questions

5 votes
5 votes
1 answer
3