2,064 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
306
306 views
khanshahima4 asked Sep 24
306 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 ...
14 14 votes
1 1 answer
1.4k
1.4k views
GO Classes asked Jul 2
1,375 views
Assume:$\texttt{short = 2 bytes}$, $\texttt{int = 4 bytes}$, $\texttt{char = 1 byte}$$\texttt{int}$ needs $4$-byte alignment and the final structure size is rounded to a ...
18 18 votes
2 2 answers
766
766 views
GO Classes asked Jul 2
766 views
Assume:struct Student { double gpa; }; struct Student alice; struct Student *sptr = &alice;Which of the following correctly assigns $\texttt{4.0}$ to $\texttt{alice.gpa}$...
10 10 votes
1 1 answer
570
570 views
GO Classes asked Jul 2
570 views
Consider the following code idea from the source:typedef struct { double x, y; } Point; void reset(Point p) { p.x = p.y = 0; } int main() { Point a = {12.0, 42.0}; Point ...