2,068 views
4 4 votes
 

Which of the statements is correct about the program?

#include<stdio.h>

int main()
{
    float a=3.14;
    char *j;
    j = (char*)&a;
    printf("%d\n", *j);
    return 0;
}
  A.
It prints ASCII value of the binary number present in the first byte of a float variable a.
  B. It prints character equivalent of the binary number present in the first byte of a float variable a.
  C. It will print 3
  D.
It will print a garbage value

1 Answer

Best answer
1 1 vote

Here we are given 'j' which is a pointer to a variable of type 'char' and in other words hence contains address of a data which is of 'char' type..

Now   j  =  (char*) &a  assigns the address of a to 'j' but after doing typecasting...

The significance of typecasting means the data stored in memory address of 'a' will now behave as 'char' .Specifically now if we access data by dereferencing 'j' , the data will be accessed one char at a time ..Normally this is 1 Byte..So it will be accessed 1 B at a time..Same holds true for incrementation as well..

Data type float occupies 4 B of data..But now with the typecasting to char , the data stored in address of 'a' will be accessed bytewise starting from lowermost byte following Little Endian mechanism[Lower byte stored in lower address]...

Now we have : 

printf("%d\n" , *j) ;

So we are printing integer equivalent of the concerned byte which is nothing but ASCII equivalent of that byte(char)..

Hence A) should be the correct answer..

• selected by
Position:
Show:

Related questions

1 1 vote
1 1 answer
337
337 views
khanshahima4 asked Sep 24
337 views
1. If precedence tells me which operator has priority, and associativity tells me how to handle operators at the same level, then why do I need a separate concept called ...