C - Floating-Point Numbers


Floating-point numbers hold values with a decimal point, so you can represent fractional as well as integral values.

The following are examples of floating-point values:

1.6    0.00008    1234.899 100.0

The last constant is integral, but it will be stored as a floating-point value.

Floating-point numbers are often expressed as a decimal value multiplied by some power of 10, where the power of 10 is called the exponent.

For example, each of the examples of floating-point numbers above could be expressed as shown in Table 2-6.

Value With an exponent Can also be written in C as
1.60.16 x 101 0.16E1
0.000080.8 x 0.0001 0.8E-4
1234.899 0.1234899 x 10000 0.1234899E4
100.0 1.0 x 100 1.0E2

Floating-Point Variables

Floating-point variable types only store floating-point numbers.

You have a choice of three types of floating-point variables.

Keyword Number of bytes Range of values
float 4 +/-3.4E+/-38 (6 to 7 decimal digits precision)
double 8 +/-1.7E+/-308 (15 decimal digits precision)
long double 12 +/-1.19E+/-4932 (18 decimal digits precision)

You declare a floating-point variable in a similar way to an integer variable.

float radius;
double biggest;


To write a constant of type float, append an f to the number to distinguish it from type double.

You could initialize the previous two variables when you declare them like this:

float radius = 2.5f;
double biggest = 123E30;

To specify a long double constant, you append an uppercase or lowercase letter L.

long double huge = 1234567.89123L;

Related Topics