C Programming

What is Double in C

In C programming, there are different data types and each variable has its associated data type. Each data type has specific memory and specific functions can be performed over it. The data type in C describes what kind of data a variable can hold like integer, character, float, and double. 

In C, we often encounter large values in terms of precision and these values cannot be stored in float, for that C has a data type double that is used to store high-precision floating-point. 

We will discuss the double data type in C in this guide.

What is double in C?

The double data type is used to store the decimal values with double precision and is capable of holding 64 bytes of decimal numbers and has more precision compared to the float. There can be 17 values before and after the decimal points in the double data type. Different data types have different ranges for storing the values in the memory. The double required 8 bytes of memory, and the format specifier is %lf. The long double required 12 bytes or 16 bytes depending on the compiler and the format specifier is %Lf.

Declaration of double Value in C

In C, the double variables are declared using the double keyword followed by the variable name:

double varName;

Where varName is the variable name.

Initialize double Value

In C, there are two ways to initialize the double value, one is you can assign the value to the declared variable, or you can assign the value to the variable at the time of the declaration.

double number;

number = 15678.975;

Or:

double number = 15678.975;

Print Double Value

In C programming, the format specifier %f is used to print the values of a float, while %lf is used to print the values of a double. Both can be used for printing the values of double, but it’s recommended to use %lf to ensure accurate printing of double values. The printf() function in C treats both the same, but it’s good practice to use the correct format specifier for the data type you are printing to avoid any potential issues.

Consider the below example program for initializing the double value and printing them in C:

#include <stdio.h>

int main()

{

    double x = 187967.450;
    double y = 15098.564;
    double z = 90657.189463;
    printf("The double value of x is:%lf\n", x);
    printf("The double value of y is:%lf\n", y);
    printf("The double value of z is: %lf\n", z);
 
    return 0;


}

Output


Let’s consider another example for printing a double value in C using %f, %lf and %Lf with width and precision. This example will clearly differentiate the purpose of using different specifiers for printing large values.

#include <stdio.h>

 

int main() {

    double x = 3.14159265359;
    long double y = 123456789.123456789;
    // Using %f to print double value
    printf("Using %%f for double: %f\n", x);
    // Using %lf to print double value
    printf("Using %%lf for double: %lf\n", x);
    // Using %Lf to print long double value
    printf("Using %%Lf for long double: %Lf\n", y);
    // Using %f to print long double value
    printf("Using %%f for long double: %f\n", y);
    // Using %f with width and precision
    printf("Using %%f with width and precision: %20.15f\n", y);
    // Using %lf with width and precision
    printf("Using %%lf with width and precision: %20.15lf\n", y);
    // Using %Lf with width and precision
    printf("Using %%Lf with width and precision: %20.15Lf\n", y);

    return 0;


}

Final Thoughts

In C, the double is a double precision floating pointing data type that can hold 64-bit floating points. This data type is equal to 8 bytes and bigger than the float data type and can store up to 15 to 17 digits. However, it may depend on the compiler you are using. Users can handle huge data sets and mantissa for precision by understanding the double data type.

About the author

Zainab Rehman

I'm an author by profession. My interest in the internet world motivates me to write for Linux Hint and I'm here to share my knowledge with others.