I am trying to get the length of a float, but it is saying the length is 8. I want to know how many digits there are. why is it saying that there are only 8 digits?
#include <stdio.h>
int main()
{
double n = 72382.413651;
int len;
len = sizeof(n);
printf("%d\n", len);
}
I am needing to know the length of the float because it will be beneficial in making a table table of logarithms like this.
--------------------------
| 1000000 | 72382.413651 |
--------------------------
Use of
sizeof(n)is somewhat misleading, because the value ofndoes not play into the answer: you always get the equivalent ofsizeof(double), which appears to be 64 bits or 8 bytes on your computer. Recall thatsizeofis a compile-time operation; only the type of the parameter matters, but not its value.The number of digits in a float's representation is often a meaningless value, because in general the representation is imprecise. Only the number of digits before the decimal separator can be determined reliably; the digits after the separator are usually truncated to the number that you set to your liking.
Rather than finding out the size of your
floats, force them all to a specific length. Specify width and precision in your calls ofprintfto make sure that everything is formatted to the same length, and looks good in a table.