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.

MrMino said:   8 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!

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

Appaso said:   9 years ago
What is char* and *char?

ASHISH GOPAL said:   9 years ago
About heap memory:

Whenever we use dynamic memory allocation functions such as malloc() or calloc(), it allocates a memory and returns a pointer to it. The allocation of the memory is done in internal RAM of the micro controller, this memory is nothing but the heap memory.

Ichidan said:   9 years ago
The answer is incorrect.

'It prints the 8-bit signed integer equivalent of the data present in the first byte of the float variable a'.

1. It's got nothing to do with ASCII. As ASCII is just a standard way of representing a 7-bit number i.e. numbers 0 - 127 are mapped to various symbols. e.g. 48 = '0', 65 = 'A', 66 = 'B', 97 = 'a'.

2. It's got as much to do with binary as it's got to do with transistors.

Dilip said:   9 years ago
Static and global variables are stored in Data section of memory.

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.

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

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?

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.


Post your comments here:

Your comments will be displayed after verification.