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

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.
(2)

Bindu said:   8 years ago
Here this program produces garbage value.

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

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

This code is similar. But the output is 3. why? integer is also 4 byte. Here ASCII value is not printed.

Raja said:   1 decade ago
It prints -61 that is garbage.

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.

Himanshu Chauhan said:   1 decade ago
Hey can any one please explain me that what is the main difference between printf and scanf?

ASHISH said:   1 decade ago
What is happening here is that ASCII value of a character that looks like '|-' or a 'T' rotated by 90 degrees anti-clockwise is getting stored in the
first byte of the float variable a.

Now the ASCII value of this character actually is 195. But since 1 signed byte stores 8 bits. The first bit is for sign which leaves us with 7 bits to
store the ASCII value. So the range is -128 to 127. Since 195 > 127. 195 in binary form is 1100 0011.

NOW WHAT IS VALUE OF 195 IN UNSIGNED CHAR RANGE.....
The MSB i.e 1 is used as sign. MSB=1 indicates negative.

X100 0011
||
X011 1100<--1'S COMPLEMENT
+1<--2'S COMPLEMENT
------------
X011 1101 (32+16+8+4+1=61).
------------

NOW CONSIDERING MSB BIT 1, THE VALUE OF 195(1100 0011) IN UNSIGNED CHAR WILL 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?

Balashankar said:   10 years ago
Float value is stored in memory as IEEE754 format. This is different from normal binary conversion.

Madhu said:   10 years ago
@Manasa.

Here float a = 3.

Hence in its 4 bytes it has its values as:

00000000 00000000 00000000 00000011.

As it is converted to char, its size is 1 byte and it points to first byte, whose value is '0'. Hence result is zero.


Post your comments here:

Your comments will be displayed after verification.