C Programming - Pointers - Discussion

Discussion Forum : Pointers - Point Out Correct Statements (Q.No. 3)
Which of the statements is correct about the program?

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
No answer description is available. Let's discuss.
50 comments Page 1 of 5.

Mukta said:   3 years ago
@HImanshu Chauhan.

printf prints it in a format you want and scanf accepts whatever you enter.

Parmeshwar said:   5 years ago
It's a garbage value -61 ... And the float.
It's a cycle of signed range.... -127 to 127.

Sarvesh karan said:   5 years ago
Float a=3.14;

All variables are stored in memory in binary representation only.
Like int a=2; four bytes will be occupied in memory where first byte will be stored as 2 or 10 in binary and rest 3 bytes will be stored as 0.
Similarly, float quantities are also stored in binary representation

Sign exponent mantissa. And this information is spread among 4 bytes.

Since here we are type casting it to char*
We are informing compiler to take 1st byte out of 4 bytes of the float.

So we get the partial value of 3.14 mantissa in the first byte of that float variable.

Now y is negative value.
While printing we are again taking 4 bytes of that variable, where only one byte is having data rest all are 0.

As it was typecast to char last byte is holding now 195,
A signed Char ranges between -128 to 127.
So after 127, it will repeat cycle from negative most quantity..i.e -127 -126 -125 .... -61.

Noel Nosse said:   6 years ago
This problem/code appears to me to have been written with errors.

The %d in the printf does not match the float or char. If you make both the value (which is float 3.14) AND the pointer a float AND change the %d to %f, you get the meaningful printing of the 3.14.

Can anyone help me to get it of?

Zdd said:   6 years ago

3.14D = 11.0010001111010111B.

S: it's 0, it's a postive
E: 10000000 (1+127), the index part is 1.E is positive, hence the base's decimal point move to the right by 1 digit, otherwise, move to the left.
M: (1.)10010001111010111here omitted a 1. in the left.

Then The decimal point is shifted to the right by 1 digit, i.e. 11.0010001111010111B i.e. 3.14D.

Hence it is stored as 01000000 0100100 11110101 11000011 (the width of float is 4 bytes).

Pranali said:   7 years ago

ASCII value is in the form of integer. And for printing decimal integer we have used %d format specifier. Got it?

Siri said:   8 years ago
but we are printing %d with %d how ASCII value will print?

Jason said:   8 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.

Ganga said:   8 years ago
Why we can write char*. Why we can write?

Please explain.

Praveena said:   8 years ago

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?

Post your comments here:

Your comments will be displayed after verification.