in Programming in C edited by
7,451 views
7 votes
7 votes

What will be output of the following program? Assume that you are running this program in little-endian processor.

#include<stdio.h>
int main()
{
    short a=320;
    char *ptr;
    ptr=(char *)&a;
    printf("%d",*ptr);
    return 0;
}
  1. 1
  2. 320
  3. 64
  4. Compilation error
in Programming in C edited by
by
7.5k views

2 Answers

14 votes
14 votes
Best answer
 short a=320;

how a will be represented in binary

   HB                                                                                LB

00000001 01000000

Now according to Little-endian .The least significant byte (LSB) value , is at the lowest address. The other bytes follow in increasing order of significance.

Now

 ptr=(char *)&a;

a is type casted to char so according to Little endianness .The least significant byte (LSB) value , is at the lowest address.

So ptr will be stored like this

   LA

 01000000

   So when the printf is executed it will print value 64.

So C is correct.

selected by

4 Comments

@Arjun Sir why here little endian store least significant byte , not most significant byte?

In little endian there is an address ordering . So how starting address will be same for little endian too?
0
0

srestha let value   ABCDE.....we want to store...in memory   in this MSB = A , LSB = E

BIG- ENDIAN let addressres be 00 ,01,02.....(though it is in binary format in computer but for clearer view assume adrress in decimal here)

00 A
01 B
02 C
03 D
04 E

LITTLE ENDIAN

00 E
01 D
02 C
03 B
04 A

address is only in increasing order...but the values that we store at locations need order according to mechanism  we follow..

1
1

@habedo007 because "short is of size 2B" while it's type-casted to "char which is of 1B".

1
1
4 votes
4 votes
answer is c)64

1 comment

explain plz.
0
0
Answer:

Related questions