Lesson 2.3. Floating point numbers

Float type

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.

Encoding of floats

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:

Format codes

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


Exercise 1

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?

Exercise 2

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

    // Display the golden number

    return 0;

Exercise 3

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)

  // Display the variables

  return 0;


Which types are used to store decimal numbers?

Check Bravo! The long float type does not exist. Try again...

How much space does a long double take in memory?

Check Bravo! A long double requires 16 bytes (128 bits) of memory. Try again...

Which type allows to memorize the greatest number?

Check Bravo Obviously, the 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?

Check Bravo! A float is not enough and neither is a long int. Try again...

Which type will be the most precise?

Check Good for you! Thanks to the floating point mechanism, the precision increases with the number of bits of the type. Try again...

To store a number between -100 and +100, what is the advantage of using a double rather than a float?

Check Bravo ! An 8-byte double will be more accurate than a 4-byte float. Try again...

See also

Last update : 11/21/2022