# Overview

• `float` and `double` are both floating-point types
• `float` generally has less precision than `double`
```float f = 1.0e9;
double d = 1.0e9;
f += 123.456789;
d += 123.456789;
printf("%f\n", f);
printf("%f\n", d);
```
```1000000128.000000
1000000123.456789
```

# Calculations

Calculations are generally carried out with `double` precision.

```float f = 1.0e9;
double d = f + 123.456789;
printf("%f\n", d);
```
```1000000123.456789
```
• The calculation `f + 123.456789` must have used double precision.

# scanf

`scanf` needs to know the difference between `float` and `double`:

```float f;
double d;
scanf("%f %lf", &f, &d);
```
• To scan a `float`, use `%f`.
• To scan a `double`, use `%lf` (think “long float”).

# printf

• Remember how all `float` expressions are evaluated using `double` precision?
• Similarly, `float` arguments to functions are promoted to `double`.
• Therefore, `printf` can’t tell the difference between a `float` and a `double` argument.
• Therefore, `%f` and `%lf` work for both `float` and `double` in `printf`.
```float f = 1.2;
double d = 3.4;
printf("%f\n", f);
printf("%f\n", d);
printf("%lf\n", f);
printf("%lf\n", d);
```
```1.200000
3.400000
1.200000
3.400000
```

User: Guest

Check: HTML CSS
Edit History Source

Modified: 2018-02-26T12:14

Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA