A double in C is a double-precision floating-point type: on typical systems it takes 8 bytes and holds about 15 to 16 significant decimal digits. Declare one with double price = 19.99;, print it with printf("%f", price), and read it with scanf("%lf", &price).
The sizes and digit counts below are for IEEE 754 systems, which covers the common desktop and server platforms. The C language itself only says double matches the IEEE 754 binary64 format "if supported", so check your compiler if you target something unusual.
How to declare and use a double in C
A double is declared like any other variable. A number with a decimal point and no suffix is already a double literal, so no special syntax is needed:
#include <stdio.h>
int main(void)
{
double radius = 2.5;
double area = 3.14159265358979 * radius * radius;
printf("Area: %f\n", area);
printf("Area, 2 decimals: %.2f\n", area);
return 0;
}
Example output:
Area: 19.634954
Area, 2 decimals: 19.63
You can also write literals in scientific notation: double avogadro = 6.022e23; means 6.022 times 10 to the power 23. An f suffix (2.5f) makes the literal a float and an L suffix (2.5L) makes it a long double.
Size, range and precision of double
Microsoft's C language reference lists the properties of the two common floating-point types. The values come from the IEEE format:
| Property | float | double |
|---|---|---|
| Storage | 4 bytes | 8 bytes |
| Significant digits | 6 to 7 | 15 to 16 |
| Exponent bits | 8 | 11 |
| Mantissa bits stored | 23 | 52 |
| Smallest normalized value | about 1.175E-38 | about 2.225E-308 |
| Largest value | about 3.403E+38 | about 1.798E+308 |
The leading bit of the mantissa is implied rather than stored, so the effective precision is 24 bits for float and 53 bits for double. Those 53 bits are where the 15 to 16 decimal digits come from. They also explain one hard limit: a double can hold every integer exactly up to 253, which is 9,007,199,254,740,992. Above that, some integers cannot be represented, and adding 1 may change nothing.
Your compiler exposes the exact values for its platform in <float.h>: DBL_DIG (decimal digits that survive a round trip), DBL_EPSILON (the gap between 1.0 and the next larger double, 2-52 or about 2.22E-16 on IEEE systems), DBL_MIN and DBL_MAX. Use those instead of hard-coding numbers.
printf and scanf format specifiers for double
Printing and reading are where most beginners trip up, because the two functions treat the type differently.
| Specifier | With printf | With scanf |
|---|---|---|
%f | Decimal notation for a double (a float is promoted). Default is 6 decimal places. | Expects a float *. |
%lf | Same as %f since C99. | Expects a double *. |
%e | Exponent notation such as 1.500000e+00. | Accepted, reads the same as %f. |
%g | Picks %f or %e style and drops trailing zeros. | Accepted, reads the same as %f. |
%Lf | For a long double. | Expects a long double *. |
The rule in short: for printf you can use %f for both float and double, because every float argument is promoted to double in a variadic call. For scanf you must use %lf for a double, because it writes through a pointer and has to know how many bytes to store:
double temperature;
printf("Enter a temperature: ");
if (scanf("%lf", &temperature) != 1) {
fprintf(stderr, "Not a number\n");
return 1;
}
printf("You entered %.1f\n", temperature);
Checking that scanf returned 1 matters: it returns the number of values it read, and on bad input your variable is left unchanged. Control the number of decimals with a precision, as in %.2f, and the minimum width with a number before the dot, as in %8.2f.
Why a double gives the wrong answer: precision errors
A double stores a binary fraction. Values such as 0.5 and 0.25 are exact, but 0.1 is not, in the same way 1/3 cannot be written exactly in decimal. Print more digits than the default and you can see the stored value:
#include <stdio.h>
int main(void)
{
double a = 0.1;
double sum = 0.1 + 0.2;
printf("%.17g\n", a);
printf("%.17g\n", sum);
printf("%d\n", sum == 0.3);
return 0;
}
Example output on an IEEE 754 system:
0.10000000000000001
0.30000000000000004
0
The default %f prints only 6 decimals, so it hides this. The comparison sum == 0.3 is false. Do not compare doubles with == after arithmetic. Compare the difference against a tolerance chosen for your data:
#include <math.h>
#include <float.h>
int nearly_equal(double left, double right, double tolerance)
{
return fabs(left - right) <= tolerance;
}
/* nearly_equal(0.1 + 0.2, 0.3, 1e-9) is true */
A fixed tolerance such as 1e-9 suits numbers near 1. For very large or very small values, scale the tolerance to the magnitude of the numbers, and use DBL_EPSILON as the lower bound for what counts as a real difference.
For money, avoid double. Rounding errors accumulate over many additions. Store amounts as integer cents in a long long and convert only for display.
Mixing int, float and double
When you mix types in one expression, C converts to the more precise type. Microsoft's reference gives the example of a float multiplied by a double: the float is promoted, the multiplication happens in double, and the result is rounded back if you assign it to a float. Integer division is a separate trap:
int total = 7;
int count = 2;
double wrong = total / count; /* 3.0: integer division happens first */
double right = (double) total / count; /* 3.5 */
The division total / count is done on two int values and truncated to 3 before it is stored in the double. Casting one operand first makes the whole division floating-point.
When to use float, double or long double
Default to double. It has about twice the digits of float, and the standard math functions (sqrt, sin, pow and the rest of <math.h>) take and return double. Use float when memory or bandwidth matters more than precision, for example large arrays of sensor samples or graphics data. Use long double only when you need the extra range or precision and know your target: it is the 80-bit x87 format on many x86 systems, a 128-bit format on some others, and the same as double in Microsoft's compiler.
If you are comparing C with another language, the same IEEE 754 double is what Python calls float, covered in Python float type explained. For a wider view of how types differ across languages, see data types in programming explained.
If the output looks wrong
- Prints
0.000000or garbage. You may have used%dwith adouble, or%fwith anint. The specifier must match the argument type. Compile with-Wallin GCC or Clang to get a warning. - scanf stores nonsense in a double. You used
%finstead of%lf. - Result is 3 instead of 3.5. Integer division, as shown above. Cast one operand to
double. - Large numbers lose their last digits. You are past 15 to 16 significant digits, or past 253 for whole numbers. Use a 64-bit integer type if you need exact whole numbers.
Frequently asked questions
- What is the difference between float and double in C?
- Both store numbers with a fractional part, but double has more storage and precision. On IEEE 754 systems float uses 4 bytes and gives about 6 to 7 significant digits, while double uses 8 bytes and gives about 15 to 16. Double also has a far larger range, up to about 1.8E+308 against about 3.4E+38 for float.
- Why does printf use %f for a double but scanf needs %lf?
- printf receives a float promoted to double, so %f and %lf both mean double there. scanf stores through a pointer, so the pointer type matters: %f expects a float pointer and %lf expects a double pointer. Using %f with a double variable in scanf writes the wrong number of bytes.
- Why is 0.1 + 0.2 not exactly 0.3 in C?
- Double stores numbers as binary fractions, and 0.1, 0.2 and 0.3 have no exact binary form. Each is rounded to the nearest representable value, and the small errors show up when you print more digits or compare with ==. Compare with a tolerance instead.
- Is long double the same as double?
- Not necessarily. The size of long double depends on the compiler and platform. It can be the 80-bit x87 format, a 128-bit format, or identical to double. Microsoft's Visual C++ compiler makes long double the same format as double.