C Programming - Pointers - Discussion

Discussion Forum : Pointers - Point Out Correct Statements (Q.No. 3)
3.
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;
}
It prints ASCII value of the binary number present in the first byte of a float variable a.
It prints character equivalent of the binary number present in the first byte of a float variable a.
It will print 3
It will print a garbage value
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
50 comments Page 2 of 5.

Jason said:   9 years ago
IEEE 754 floating point representation creates the following 32-bit pattern for decimal 3.14: 01000000010010001111010111000011.

Lowest 8 bits are 11000011. So in 2's complement, this represents -61.

Vishwas said:   1 decade ago
Here j is a character pointer, it will hold the address of size 1 byte, but float variable value(3.140000) spread in 4 bytes.

So 1st byte of the float value get printed by the character pointer j.

Prasanna Hegde said:   1 decade ago
-61 is the answer when compiled using a GNU compiler. I don't know about turbo c/c++ and I wonder why people use that even if it doesn't follow universally accepted ANSI standard!

Manasa said:   1 decade ago
#include<stdio.h>

int main()
{
float a=3;
char *j;
j = (char*)&a;
printf("%d\n", *j);
return 0;
}

Can somebody explain why the answer is "0" here.

MrMino said:   10 years ago
printf will expect sizeof (int) value at %d. printf will read the first 4 bytes of the float, and print them as integer value. It will print garbage. Correct answer should be D!

Praveena said:   9 years ago
Hello,

I have a doubt. All the pointers have same memory allocation i.e, 2 bytes in c/c++ and 4 bytes in GCC.
Then why j takes only one byte, since it is a pointer variable?

Madhureddy said:   1 decade ago
Since here type conversion from float to charecter take place. Hence the ASCII value of the binary number present in the first byte of float variable a will print.

Ganesh said:   1 decade ago
@Vishwas
which is the 1st byte in 3.140000? 03 or 00?? whatever it is.. ascii value for 0 is 60 and that of 3 is 63.. so how can ans be -61??

Devendra said:   1 decade ago
What about %d it is a specifier which prints the decimal value of the variable not the ASCII value for which %c would have been used?

Balaji said:   1 decade ago
Char takes in pointer so the first byte takes the role but print the int value so the result is the binary value of the first byte.


Post your comments here:

Your comments will be displayed after verification.