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 5 of 5.

Vipul said:   1 decade ago
What is heap?

Vipul said:   1 decade ago
Please give proper explanation.

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.

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.

Deepa said:   1 decade ago
Can anyone explain it clearly?

Sk rashid ali said:   1 decade ago
If we replace %d with %u its giving an addrress.

Dilip Sharma said:   1 decade ago
float a=3.14;
char *j;
j = (char*)&a;
printf("%d\n", *j);
return 0;
//here j is pointer to char a are declare as 3.14 which is float in third line &a is nothing but the reference of a i.e direct retrived the value 3.14 and cast it as (char*) ;
so here float value are casted.hence it will print the ASCII charactor of first byte.

Vishwas S said:   1 decade ago
Can anyone please explain how 3.14 is stored in a computer?

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??

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!


Post your comments here:

Your comments will be displayed after verification.