**Summary**: in this tutorial, you will learn about various **C float types** including float, double and long double.

## Introduction to C float types

C provides various floating-point types that represents non-integer number with a decimal point at any position. For example, with integer types, you only can have numbers `1`

, `2`

, `10`

, ` 200`

… however with floating-point type, you can have `1.0`

, `2.5`

, `100.25 `

and so on.

There are three standard floating-point types in C:

`float`

: for numbers with single precision.`double`

: for numbers with double precision.`long double`

: for numbers with extended precision.

The following table illustrates the technical attributes of various floating-point types in C. It is important to notice that this is only the minimal requirement for storage size defined by C.

Type | Size | Ranges | Smallest Positive Value | Precision |
---|---|---|---|---|

float | 4 bytes | ±3.4E+38 | 1.2E-38 | 6 digits |

double | 8 bytes | ±1.7E+308 | 2.3E-308 | 15 digits |

long double | 10 bytes | ±1.1E+4932 | 3.4E-4932 | 19 digits |

## What is precision?

If you consider the fraction 5/3, this number can be represented in decimal as `1.666666666...`

with infinite 6. When you use the floating-point type with limited bytes to store infinite number, it is impossible. Therefore the floating-point value can be stored only with a limited precision. The precision is expressed as a number of significant digits or in another way, the precision** **is defined as how many digits that a number can represent without data loss.

For example, the `float `

type with 4 bytes = 4 x 8 bits = 32 bits, is for numbers with single precision. It means that a float gives 1 sign bit, 8 bits of exponent and 23 bits of significand.

The `double `

type is for number with double precision that gives 1 sign bit, 11 bits of exponent and 52 bits of significand. (8 bytes x 8 bits = 64 bits = 1 bits + 52 bits + 11 bits).

## Float ranges and precision

In order to find the value ranges of floating-point number in your platform, you can use the ` float.h `

header file. This header file defines macros such as `FLT_MIN, FLT_MAX`

and `FLT_DIG `

that store the float value ranges and precision of the `float `

type.

You can also find the corresponding macros for `double `

and `long double `

with the prefixes `DBL_ `

and `LDBL_`

The following program illustrates the storage size and precision of floating-point numbers in your system.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | /* * File : main.c * Author : zentut.com * Purpose: C float demo */ #include <stdio.h> #include <float.h> int main( ) { printf("Storage size: %d bytes\n" "Minimum float value: %E\n" "Maximum float value: %E\n" "Precision: %d decimal digits\n", sizeof(float), FLT_MIN, FLT_MAX, FLT_DIG); puts("\nExample of float precision:\n"); double d = 12345.6; float f = (float)d; printf("The floating-point number d " "%18.10f\n", d); printf("Stored in a variable f\n" "of type float as the value " "%18.10f\n", f); return 0; } |

The output of the program in our system is as follows:

1 2 3 4 5 6 7 8 9 10 | Storage size: 4 bytes Minimum float value: 1.175494E-038 Maximum float value: 3.402823E+038 Precision: 6 decimal digits Example of float precision: The floating-point number d 12345.6000000000 Stored in a variable f of type float as the value 12345.5996093750 |

In this tutorial, we have introduced you various C float types and explained what precision means to each type of float in C.