in Programming in C edited by
7,460 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

9 Comments

Why are only 7 bits being stored in a byte?
0
0

Hi @ ManojK     can you explain what would be the output of this program if we remove the little-endian processor.   condition  would that be changed ? or be same as it is .

0
0
Just run the program .
0
0
@shekhar obviously. It will then print the most significant byte. But big-endian machines are rare.
3
3

@arjun sir and @manojK I ran it again without the condition and o/p is still 64

but my confusion is in there 2 lines

  ptr=(char *)&a;
    printf("%d",*ptr);

a has a value 320 . in first line we are type casting  the address of the value  a , just for example let say the address of a is 100 (this is what i understood from this how do i know what is the address of a and what would be the value of it after type casting into char) now first line typecasting that short value in char and storing into ptr .now how am i getting 64 ?

the compiler did not say anything why it gave 64 to me.

0
0

 let say the address of a is 100 (this is what i understood from this how do i know what is the address of a and what would be the value of it after type casting into char)

It'll be still 100 - address has same value for ant type. Type casting here is for ensuring type safety. 

now how am i getting 64

Due to *ptr as type of ptr is char *. Try *(int*)ptr. 

0
0
@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