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.

Madhu said:   1 decade 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.

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

Ichidan said:   1 decade 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.

ASHISH GOPAL said:   1 decade 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.

Amit said:   1 decade ago
How is the ASCII value of 3.14 found?

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

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?

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

Please explain.

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.


Post your comments here:

Your comments will be displayed after verification.