You are here:

C/float storage

Advertisement


Question
hello Narendra
i want to ask about how float stores in memory?
because after seeing this program i am in great
confusion
float x=0.7;
if(x<0.7)
   printf(" c");
else
   printf("c++");
o/p is c .
but

float x=0.8;
if(x<0.8)
   printf(" c");
else
   printf("c++");
ans is c++
why is it so?

pls explain this ?

monika  

Answer
The reason for this is, the way float is represented in memory.
Usually IEEE standard is used for representation.
In that there are 3 parts: Mantissa, Exponent and sign.
So, when you say 0.7, it could have been represented as 0.000006999999 * (10 ^ 5).

So, no float number is represented as you see it on your computer.

Here are some url's, which explain about this in detail:
1. http://scholar.hw.ac.uk/site/computing/topic21.asp?outline=
2. http://wiki.tcl.tk/13206
3. http://community.borland.com/article/0,1410,15855,00.html
4. http://steve.hollasch.net/cgindex/coding/ieeefloat.html
5. http://www.markworld.com/showfloat.html
6. http://en.wikipedia.org/wiki/IEEE_floating-point_standard

In the 5th url, you can actually enter a number and see how it gets split to different parts.
And the last one is a wiki page on the subject.

Hope this helps and clarifies.

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Narendra

Expertise

I can answer questions in C related to programming, data structures, pointers and file manipulation. I use Solaris for doing C code and if you have questions related to C programming on Solaris, I will be able to help better.

Experience

6.5

Organizations belong to
Sun Microsystems

Awards and Honors
Brain Bench Certified Expert C programmer.
Advanced System Software Certified

©2012 About.com, a part of The New York Times Company. All rights reserved.