C float & Double Precision Data Types

Like the integer data type, float and double both are used for numerical operations. An integer does not work with the fractional part but float and double do. In this lesson, we will learn the float and double data types in detail.

Float data type also known as floating-point numbers and double data type also known as double precision. For the floating-point number, C uses float as the keyword itself and also uses double for double-precision numbers as the keyword itself.

Like integer data type, we have to keep the fractional numbers in the memory addresses. And we will use float and double keywords for putting fractional numbers in the memory addresses. And this is called variable declaration that we learned in a previous lesson. Now look at the following syntax that consists of declaring float and double numbers (fractional numbers):

Note: The difference between float and double data type is:
# Float is a 32bit single precision Floating Point Number. Which takes 1 bit for the sign bit, 8 bits for exponent part, and it has 7 decimal digits of precision.
# Double is a 64bit double precision Floating Point Number. Which takes 1 bit for the sign bit, 11 bits for exponent part, and it has 15 decimal digits of precision.
So logically float and double data types are the same but they are different in memory size.

Example:

#include <stdio.h>
int main() {
    /*declaring float and double data without initialization*/
    float a;
    float b, c;
    double d;
    double e,f;

    float g=10.434, h=5.876; //initializing float while declaring
    double i=43.43, j=3.45, sum; //initializing double while declaring

    /* initializing value of uninitialized variable */
    a = 21.33;
    b = 2.34;
    c = 4.3;

    /* re-initializing value of initialized variable
       or changing value of a existing float and double value */
    h = 45.43;
    i = 5.43;
    sum = i + j; //simple arithmetic

    /* printing values of float & double variables */
    printf("Value of a: %f \n", a); // %f used as format specifier for float
    printf("Sum of i and j: %lf \n", sum); // %lf used as format specifier for double
    
    /* Arithmetic in the printf function */
    printf("Sum of a and b: %f \n", a+b);
    
    /* Limiting the fractional part with specific numbers */
    printf("Value of a: %0.2f \n", a);
    
    /* you can use 0.1, 0.2, 0.3, etc for
    showing corresponding fractional part */
    
    return 0;
}

In the above single program, we tried to show all the basic syntax of declaring float and double data type or variables. Now if you compile and run the code, you should get the following;

Output:

Value of a: 21.330000
Sum of i and j: 8.880000
Sum of a and b: 23.670000
Value of a: 21.33

For practicing you should write code randomly and run them for mapping output for the corresponding code.

C Floating Points modifiers, size and value ranges

Like integer data type, floating-point has also some modifiers and by using them you can increase the memory size and value ranges. Floating-point numbers have no unsigned types.

Data Types Memory size Value Ranges Precision Placeholder
float 4 bytes 1.2E-38 to 3.4E+38 6 decimal points %f
double 8 bytes 2.3E-308 to 1.7E+308 15 decimal points %lf
long double 10 bytes 3.4E-4932 to 1.1E+4932 19 decimal points %lf

« Previous Next »