Hit enter after type your search item

C++ Double [Declaration, Limits, size etc.] with 5 Examples

What is C++ Double data type?

  • The double is among one of the available data types in C++ like float, int, char etc.
  • Double data type allows storing bigger floating point numbers (decimal numbers) than the float data type.
  • It takes eight bytes (64 bits) in the memory whereas float takes four bytes.
  • Generally, it’s precision is 15 digits.

How to declare a double variable in C++?

You may declare double variables in these ways:

double var;

double var, var2, var3;

double var = 500.987678767;

double var = -500.877878787;

double var = 500;

float var = 87; //Declaring a float type variable without suffix f or F is taken as double type as well.

C++ Program for declaring and using double type variables

In this C++ program, we will create a few double type variables and display their values by using cout. Have a look:


Output:

CPP-double

Check the memory size of double variables example

As mentioned earlier, a double variable takes eight bytes in memory. The example below shows the actual size by using the sizeof() function.

See the program and its output below:


Output:

CPP-double-size

Checking the minimum and maximum limits of the double type variables example

The example below shows using the numeric_limits to check the minimum and maximum limits of the double type variables. You have to include the <limits> in the header.

Note that, the output varies from compiler to compiler – where you execute this code:


Output:

CPP-double-limits

Checking and comparing the precision of double types

The following example checks the precision of double type variables and compares it with other floating type data types like float and long double.


Output:

CPP-double-precision

Now let us use the setprecision() function and see the result


CPP-double-set-precisi

You saw, the values for double and long double are displayed correctly with the 11 precision setting. However, the float is showing garbage value after 7.

This div height required for enabling the sticky sidebar