in Programming in C retagged by
537 views
8 votes
8 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$
in Programming in C retagged by
537 views

4 Comments

@GO Classes Please provide the solution for this question

 

0
0

$ \large{\colorbox{yellow}{Detailed video solution of this question with direct time stamp}}$

All India Mock Test 4 - Solutions Part 1

0
0

@Sachin Mittal 1 sir 
same type pointer points to same type is valid.
void or char points to other type is valid..
all other gives undefined behaviour.

so it may give undefined behaviour also..

0
0

@krishnajsw

You can ask any type of pointer to point any other type, as long as you typecast, it is valid.

Please refer to Stanford Lectures here

1
1

2 Answers

6 votes
6 votes

assuming int is 4B

255 in big endian 00000000 00000000 00000000 11111111

access first 2B, 00000000 00000000 = 0

 

255 in little endian 11111111 00000000 00000000 00000000

access first 2B, 11111111 00000000 = this is 255 in 2B,

 in little endian we have least significant byte in lower address in binary it become 0000000011111111=255

 

little endian variations

if insted of i=255 it was i=65535, it will result in -1

because in memory it will be 11111111 11111111 00000000 00000000

with short we will access the first 2B 11111111 11111111 which is -1 in $2’s$ compliment in 2B

and if in this case we use “unsigned short*” we will get 65535 as we are considering the first 2B as unsigned

edited by
0 votes
0 votes

Hope it helps

Answer:

Related questions