Dremendo Tag Line

Float and Double Variables with Differences in C++

C++ Basic Concepts

In this lesson, we will learn, what is Float and what is Double variable in C++ programming. We will also look at some examples and will take a quiz at the end.

Float and Double Type Variables

As described before, an integer variable value has no fractional (decimal) part. C++ uses two types of variables float and double that can store a decimal number. They each offer a different level of precision (ability to store numbers after the decimal point) as outlined below.

  • float - A float data type variable can store about a maximum of 6 digits of precision. The memory capacity of the float data type is 4 bytes and can store a number between range 1.2E-38 to 3.4E+38.
  • double - A double data type variable can store about a maximum of 15 digits of precision. The memory capacity of the double data type is 8 bytes and can store a number between range 2.3E-308 to 1.7E+308.

Note: The main difference between float and double data type is their memory capacity, precision and number range as outlined above.

video-poster

Syntax of Declaring Float and Double Variable in C++

float variable_name;
double variable_name;

Here float is used for declaring Float data type and double is used for declaring Double data type. variable_name is the name of variable (you can use any name of your choice for example: a, b, c, alpha, etc.) and ; is used for line terminator (end of line).

Now let's see some examples for more understanding.

Example 1

Declare a float variable x and a double variable y

float x;
double y;

Example 2

Declare a float variable x to assign decimal number 18.286 and a double variable y to assign a decimal number 16722.02675585.

float x = 18.286;
double y = 16722.02675585;

Example 3

Declare 3 float variables a, b, c and 3 double variable x, y, z

float a, b, c;
double x, y, z;

Example 4

Declare 3 float variables a, b and c to assign the float value 41.23, 87.335 and 62.471 respectively after that declare 3 float variables x, y and z to assign the double value 19.2347, 22.20 and 54.458691027 respectively

float a=41.23, b=87.335, c=62.471;
double x=19.2347, y=22.20, z=54.458691027;

Example 5

Declare a float variable x and double variable y and assign the float value 12.365 and double value 79.2358 in the separate line respectively.

float x;
double y;
x = 12.365;
y = 79.2358;

Example 6

Declare a float variable x and double variable y and assign the float value 7.18 and double value 11.62254 after that change their values to 44.23 and 67.48612 respectively.

float x=7.18;
double y=67.48612;
x = 44.23;          // now the new value of x is 44.23
y = 67.48612;       // now the new value of y is 67.48612

Note: If you want to store an integer number in a float or double type variable then you can do so as because int is smaller than float and float is smaller than double in terms of storing capacity of numbers. So an integer value can be stored in float type variable and a float value can be stored in a double type variable. Let's see the example given below.

float x=58;
double y=76;

The integer value 58 will be converted into float data type value and will be stored as 58.0. Similarly the integer value 76 will be converted into double data type value and will be stored as 76.0.

Test Your Knowledge


You Scored :

Percentage :