C Programming - Pointers - Discussion

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;
}

[A]. It prints ASCII value of the binary number present in the first byte of a float variable a.
[B]. It prints character equivalent of the binary number present in the first byte of a float variable a.
[C]. It will print 3
[D]. It will print a garbage value

Answer: Option A

Explanation:

No answer description available for this question.

Madhureddy said: (Feb 1, 2011)  
Since here type conversion from float to charecter take place. Hence the ASCII value of the binary number present in the first byte of float variable a will print.

Vinod said: (Apr 3, 2011)  
Can you explain how to came -61.

Durai Pandian said: (May 23, 2011)  
Please whats the meaning of the -61 as output.

Mukesh said: (Jun 3, 2011)  
Where the global variable stored in c language?

Shankar said: (Jul 7, 2011)  
GLOBAL variables are store in permanent storage area i.e static memory.

Kiran said: (Aug 31, 2011)  
Global variables are stored on heap.

Suresh said: (Aug 31, 2011)  
Can you explain how the output get -61?

Sunil Patidar said: (Sep 4, 2011)  
@kiran Global variables are stored on data segment.
the static variables are stored on heap.

Gyni said: (Sep 20, 2011)  
What is heap? can you segment and static memory storage.

Lalit Singh said: (Sep 27, 2011)  
The answer to this question is wrong it's option D.

Harry said: (Oct 5, 2011)  
Can anyone please explain ? How did this answer come ?

Priya said: (Oct 5, 2011)  
Hut the output is -61 and it is not ASCII value of a so anyone can explain this ans?

Gourav said: (Oct 7, 2011)  
C stores local variables on stack.

Global variables may be declared. These are not stack based. But stored in the data segment.

Rupinderjit Singh said: (Oct 28, 2011)  
Static variables are stored on stack and Heap is only used for dynamic memory allocation.

Vipul said: (Dec 17, 2011)  
What is heap?

Vipul said: (Dec 17, 2011)  
Please give proper explanation.

Balaji said: (Jan 5, 2012)  
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: (Jan 6, 2012)  
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: (Jan 11, 2012)  
Can anyone explain it clearly?

Sk Rashid Ali said: (Feb 18, 2012)  
If we replace %d with %u its giving an addrress.

Dilip Sharma said: (Feb 20, 2012)  
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: (Aug 22, 2012)  
Can anyone please explain how 3.14 is stored in a computer?

Ganesh said: (Sep 8, 2012)  
@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: (Jul 10, 2013)  
-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!

Shivaji Vidhale said: (Jul 21, 2013)  
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.

The MSB i.e 1 is used as sign. MSB=1 indicates negative.

Remaining (1000011)bits count to 67. After 127 the value becomes -128 since 01111111 (+127)
+ 1
------------
= 11111111 (-128)

Now add 67 till you get 195.

So -128 + 67 = 61

Amit said: (Jul 30, 2013)  
How is the ASCII value of 3.14 found?

Ritesh Agarwal said: (Jul 30, 2013)  
#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: (Aug 9, 2013)  
It prints -61 that is garbage.

Manasa said: (Aug 11, 2013)  
#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: (Nov 25, 2013)  
Hey can any one please explain me that what is the main difference between printf and scanf?

Ashish said: (Jan 13, 2014)  
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: (Apr 20, 2014)  
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: (Nov 16, 2014)  
Float value is stored in memory as IEEE754 format. This is different from normal binary conversion.

Madhu said: (Dec 8, 2014)  
@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: (Feb 27, 2015)  
Static and global variables are stored in Data section of memory.

Ichidan said: (Mar 26, 2015)  
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: (May 24, 2015)  
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.

Appaso said: (Dec 7, 2015)  
What is char* and *char?

Bindu said: (Jan 20, 2016)  
Here this program produces garbage value.

Mrmino said: (Jan 25, 2016)  
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: (Apr 8, 2016)  
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: (Sep 19, 2016)  
Why we can write char*. Why we can write?

Please explain.

Jason said: (Sep 29, 2016)  
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.

Siri said: (Dec 17, 2016)  
but we are printing %d with %d how ASCII value will print?

Pranali said: (Apr 1, 2017)  
@Siri.

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

Zdd said: (Mar 5, 2018)  
@Vishwas.

3.14D = 11.0010001111010111B.

the format of float:SEEEEEEE EMMMMMMM MMMMMMMM MMMMMMMM
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).

Noel Nosse said: (Apr 26, 2018)  
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?

Sarvesh Karan said: (Jan 28, 2019)  
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.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.