C Float Types

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

C FloatIntroduction to C float types

C provides various floating-point types that represent 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.

TypeSizeRangesSmallest Positive ValuePrecision
float4 bytes±3.4E+381.2E-386 digits
double8 bytes±1.7E+3082.3E-30815 digits
long double10 bytes±1.1E+49323.4E-493219 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 a number with the 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).

C Float

Float ranges and precision

In order to find the value ranges of the 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.

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

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

  • Was this tutorial helpful ?
  • YesNo