C provides float, double, and long double for representing floating-point values. These types differ primarily in their precision and range, with the exact representation and limits depending on the implementation.
#include <stdio.h>
int main()
{
float a = 3.14f;
double b = 3.1415926535;
long double c = 3.141592653589793L;
printf("float = %.9f\n", a);
printf("double = %.10f\n", b);
printf("long double = %.15Lf\n", c);
return 0;
}
Example Output #
The exact precision and output can vary by implementation:
float = 3.140000105
double = 3.1415926535
long double = 3.141592653589793
Explanation #
float is generally used when lower storage requirements are more important than precision:
float a = 3.14f;
The f suffix makes 3.14f a float constant.
double provides at least as much precision and range as float:
double b = 3.1415926535;
A floating-point constant without a suffix has type double.
long double provides at least as much precision and range as double:
long double c = 3.141592653589793L;
The L suffix specifies a long double constant.
Floating-Point Type Reference #
| Type | Minimum relative precision | Minimum range | Common size |
|---|---|---|---|
float |
6 decimal digits | 10⁻³⁷ to 10³⁷ |
4 bytes |
double |
10 decimal digits | 10⁻³⁷ to 10³⁷ |
8 bytes |
long double |
10 decimal digits | 10⁻³⁷ to 10³⁷ |
8, 12, or 16 bytes |
The C standard specifies minimum characteristics rather than requiring these common sizes.
Floating-Point Literal Suffixes #
| Suffix | Type | Example |
|---|---|---|
| No suffix | double |
3.14 |
f / F |
float |
3.14f |
l / L |
long double |
3.14L |
Floating-point values are not generally exact representations of decimal fractions. For example, a value such as 0.1 may require rounding when represented in a binary floating-point format.