edited by
7,532 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
edited by

2 Answers

Best answer
15 votes
15 votes
 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
Answer:

Related questions

4 votes
4 votes
4 answers
1
sourav. asked Jul 3, 2016
4,013 views
What is the output of this C code?#include<stdio.h void main() { int k=5; int *p=&k; int m=&p; printf("%d %d %d",k,*p, m); }5 5 5 5 5 junk5 junk junkcompile time error
5 votes
5 votes
2 answers
2