The floating type allows to store decimal numbers. Its ful lname is floating point numbers. There are 2 types for encoding decimal numbers in C. As for the integers, it is advisable to choose the appropriate type to minimize the use of memory usage:
Type | Bytes | Bits | Minimum | Maximum |
---|---|---|---|---|
float | 4 | 32 | ±10-37 | ±10+37 |
double | 8 | 64 | ±10-307 | ±10+307 |
long double | 16 | 128 | ±10-308 | ±10+308 |
Note: Float types do not support the unsigned
prefix. Therefore,
the unsigned float
or unsigned double
types do not exist in C.
We won't go into detail about how decimal numbers are encoded in memory. However, it is always interesting to know. If you are interested in this concept, you can consult this video which explains the details of the IEEE 754 standard.
Just remember two important things:
To go further, the video below explains the origins and consequences of these inaccuracies:
The notation of decimal numbers in C is governed by a few rules:
12.45
-24.
3.1415
0.25
can be written .25
.e
or E
, e.g. 10e6
or 10E6
.As for integers, you must use the appropriate format code to display a float. Here are the format codes for each type:
Type | Format code |
---|---|
float | %f |
double | %lf |
long double | %Lf |
Here is an example that display a double
:
#include <stdio.h>
int main(void) {
// Declare and display the variable N
double N=10e10;
printf ("%lf", N);
return 0;
}
Replace the type of the variable N
with a float
and change the format code
of the printf
. What do you notice?
Write a program that declares a variable gold_number
of type double
and initializes it with
the value 1.6180 before displaying it.
#include <stdio.h>
int main(void) {
// Declare the golden number
// COMPLETE HERE
// Display the golden number
// COMPLETE HERE
return 0;
}
Write a program that declares 2 variables a
and b
of types float
and double
respectively.
The two variables are initialized with the value \(-10^{28}\) then displayed.
#include <stdio.h>
int main(void) {
// Declare two variables (float and double)
// COMPLETE HERE
// Display the variables
// COMPLETE HERE
return 0;
}
Which types are used to store decimal numbers?
long float
type does not exist.
Try again...
How much space does a long double
take in memory?
long double
requires 16 bytes (128 bits) of memory.
Try again...
Which type allows to memorize the greatest number?
long double
allows you to store larger numbers.
Try again...
The estimated number of atoms in the universe is \( 10^{80} \). Which type is best suited to store this number?
float
is not enough and neither is a long int
.
Try again...
Which type will be the most precise?
To store a number between -100 and +100, what is the advantage of using a double rather than a float?
double
will be more accurate than a 4-byte float
.
Try again...