In C double is a keyword for the double data type. It represents floating point numbers with better precision. Usually we associate the name with the idea that it has double the precision of the float type.
Advertise on this site. I promise you will like the rates :)
Declaring a variable is just like with any other type:
double price = 9.95;
Specifier: the d specifier can be appended to the end of the value like this:
double length = 6.55d;
However, in C by default all floating point values are interpreted as double. That is why you will seldom see that being used, even though you can use it to explicitly show that this will not be a float value(float values on the other hand must be followed by the float specifier f: float height = 177.50f)
Printing uses the format specifier %lf (%lg, %le or %la are equivalent). This is the only correct way for compilers that comply with С99 or later. Anyway, many compilers still use the ANSI way and allow printing doubles with the old %f.
printf("price: %lf", price);
Reading with scanf is done with %lf with all compilers.
In C double’s exact precision depends on the implementation. Most compilers today use the IEEE-754 standard(even though most compilers do not conform to the 2019 active revision, but instead to the superseded standards from 2008 and 1985). To represent the numbers the compilers use 64 bits as follows:
It can keep up to about 16 digits before the variable is overflowed. For instance we can keep the pi number with a precision of 15 digits after the decimal point:
double pi = 3.141592653589793;
If we try to keep too much data in a variable, information will be lost. The information will be lost right to left:
This will print 9876543210.123457 for a total of 16 digits, where the last is rounded up to 7.
Normally the range and precision of double is more than enough in practice.