Double Data Type in C

The double data type in the C language is responsible for storing very large numeric values, which the float (floating point) or integer data types aren’t able to store in any given program.

In this article, we will take a closer look at the Control Statements in C according to the GATE Syllabus for CSE (Computer Science Engineering). Read ahead to know more.

Table of Contents

Why Do We Use Double Data Type in C?

The double data type or double refers to that data type in the C language that helps in storing high-precision sorts of floating-point numbers or data in the computer memory. This data type is also known as double because it is capable of holding double the size of info and data as compared to the float.

Thus, a double consists of 8 bytes of data- which is equal to a total of about 64 bits of size. Here, the double utilises 11 bits for its exponent, 1 bit for representing the sign, and all the rest of 52 bits for the mantissa of the double. The double data type ranges from 1.7E-308 to 1.7E+308.

We can also represent the double data in the form of a real number (2, 20), minus (-2, -0.00001), and decimals (0.2, 22.001).

Keep in mind that a double is capable of holding about 15 to 16 digits after and before any given decimal point (approximately).

Example: 1.9876, 5.43219, -876.543, 21.987654, 0.15197e-7, etc.

Process of Declaration and Initialization of a Double Variable in the C language

Here is how we can declare and initialize any double variable in a given C program:

Declaration

We first need to specify the variable name along with the data type for performing the declaration of the double variable in the C language.

Syntax,

double info1;

Initialization

For performing the initialization of a double variable, we need to specify any value to the variable name. This value must be valid.

Syntax,

info1 = 3.2325467;

Added to this, one can also declare the variable along with initializing it in the very same line.

Syntax,

double info1 = 3.2325467;

Examples

Let us take a look at an example where we declare and initialize the perimeter in a program.

Declaration

double perimeter1;

Initialization

perimeter1 = 40000;

Declaration and Initialization in a Single Line

double perimeter1 = 40000;

Program for Converting Centimeters into Inches with the Use of Double Data Type in C

Let us take a look at an example where we pass the available number for the double data type to a function for converting feet into meters in a program.

#include <stdio.h>

double centimeter_to_inch (double f); // declaration of a user defined type of function

int main()

{

double centimeter, cnvt; // declaration of the intended variable in the form of a double data type

printf (” We will enter the centimeter in double “);

scanf(“%lf”, &centimeter);

cnvt = centimeter_to_inch (centimeter); // calling of the centimeter_to_inch function in program

printf (” The answer for converting centimeters to inches is: %lf”, cnvt);

return 0;

}

// performing the definition of the given function

double centimeter_to_inch (double f)

{

return f / 2.54;

}

The output obtained out of the program mentioned above will be:

We will enter the centimeter in double 45.78

The answer for converting centimeters to inches is: 18.023622

Program for Converting Integer Data Type into Double Data Type in C

Let us take a look at an example where we convert an int number in the C language into a double data type number.

#include <stdio.h>

int main()

{

int num = 68, denom = 22;

double val;

val = (double) num / denom;

printf (” The conversion of an int value in C into a double data type will generate an output of : %lf \n”, val);

}

The output obtained out of the program mentioned above will be:

The conversion of an int value in C into a double data type will generate an output of : 3.09090909091

Program for Converting Fahrenheit Temperature into Celsius Temperature with the Use of Double Data Type in C

Let us take a look at an example where we perform the conversion of a temperature given in Fahrenheit to Celsius in a C program.

#include <stdio.h>

int main()

{

// the declaration of the given double variable

double f_temp, c_temp;

printf(” Please enter your temperature in Fahrenheit: “);

scanf (” %lf”, &f_temp); // accepting of the Fahrenheit temperature

c_temp = ( f_temp – 1.8 ) * 5 / 9; // using the conversion formula here

printf (” Your temperature in Celsius is: %lf”, c_temp);

return 0;

}

The output obtained out of the program mentioned above will be:

Please enter your temperature in Fahrenheit: 56.8

Your temperature in Celsius is: 13.77778

Program for Printing the Sum of Two of the Double Numbers with the Use of Function in C

Let us take a look at an example where we perform the sum of two double numbers in a C program with the use of a function.

#include <stdio.h>

double add_val(double a, double b);

int main()

{

// the declaration of given double variables in the program

double p, q, res;

printf (” Please enter two of your double numbers here “);

scanf(” %lf %lf”, &p, &q); // the program takes two double variables from its user

res = add_val(p, q); // the calling of the double function

printf (” We have found the result of the two double numbers in the program to be: %lf”, res);

return 0;

}

double sum_num(double a, double b)

{

return a + b; // the returning of the sum of the given double values in the program

}

The output obtained out of the program mentioned above will be:

Please enter two of your double numbers here 34.568

43.797

We have found the result of the two double numbers in the program to be: 78.365000

Float Data Type vs. Double Data Type: A Comparison

While some might confuse both of these data types while programming in C, there is a significant difference present between them. Let us discuss that in brief.

Float

The float data type is basically a single-precision sort of data type that is capable of holding 32 bits of decimal numbers or floating points. It equals a total of 4 bytes, and it is predefined in nature. It means that the float is a type of keyword whose name and meaning cannot be changed by a programmer throughout the program.

As a matter of fact, the float data type is much faster as compared to the double data type. It is mainly because of its small range in a program. It is capable of holding about 7 digits approximately. Also, keep in mind that a float data type basically ranges from 1.5 x 10-45 to 3.4 x 1038.

Double

The double data type is basically a precision sort of data type that is capable of holding 64 bits of decimal numbers or floating points. Thus, it is capable of storing exactly the double size of data, compared to the float data type. The double is also a predefined data type. It means that we cannot change its name and meaning throughout the program when it is running. But keep in mind that because it holds larger values, it is much slower than that of the float data type. It is very big in size, thus very slow, and it can easily accommodate about 15 to 17 digits after or before a decimal point. The typical range of a double is about 5.0 x 10-345 to 1.7 x 10308.

Practice Problems on Double Data Type in C

1. What would be the output obtained out of the program mentioned below:

#include <stdio.h>

double gram_to_ounce (double f);

int main()

{

double gram, cnvt;

printf (” We will enter the grams in double “);

scanf(“%lf”, &gram);

cnvt = gram_to_ounce (gram);

printf (” The answer of converting grams to ounces is: %lf”, cnvt);

return 0;

}

double gram_to_ounce (double f)

{

return f / 28.35;

}

A. We will enter the grams in double 28.35

The answer for converting grams to ounces is: 0.17637

B. We will enter the grams in double 5

The answer for converting grams to ounces is: 28.35

C. We will enter the grams in double 28.35

The answer for converting grams to ounces is: 5

D. We will enter the grams in double 5

The answer for converting grams to ounces is: 0.17637

Answer – D. We will enter the grams in double 5

The answer for converting grams to ounces is: 0.17637

2. What would be the output obtained out of the program mentioned below?

#include <stdio.h>

int main()

{

int total = 55, var = 12;

double mean;

mean = (double) total / var;

printf (” The mean of the total of all the values in the program is equal to : %lf \n”, mean);

}

A. The mean of the total of all the values in the program is equal to : 43

B. The mean of the total of all the values in the program is equal to : 660

C. The mean of the total of all the values in the program is equal to : 4.58333333333

D. The mean of the total of all the values in the program is equal to : 67

Answer – C. The mean of the total of all the values in the program is equal to : 4.58333333333

3. What would be the output obtained out of the program mentioned below?

#include <stdio.h>

int main()

{

double k_temp, f_temp;

printf(” Please enter your temperature in Kelvin: “);

scanf (” %lf”, &k_temp);

f_temp = ( ( k_temp – 273.15 ) * 9 / 5 ) + 32;

printf (” Your temperature in Fahrenheit is: %lf”, f_temp);

return 0;

}

A. Please enter your temperature in Kelvin: 500

Your temperature in Fahrenheit is: 440.33

B. Please enter your temperature in Kelvin: 500

Your temperature in Fahrenheit is: 408.33

C. Please enter your temperature in Kelvin: 500

Your temperature in Fahrenheit is: 540.33

D. Please enter your temperature in Kelvin: 500

Your temperature in Fahrenheit is: 376.33

Answer – A. Please enter your temperature in Kelvin: 500

Your temperature in Fahrenheit is: 440.33

4. What would be the output obtained out of the program mentioned below?

#include <stdio.h>

double add_val(double z, double x);

int main()

{

double m, n, res;

printf (” Please enter two of your double numbers here “);

scanf(” %lf %lf”, &m, &n);

res = add_val(m, n);

printf (” We have found the result of the two double numbers in the program to be: %lf”, res);

return 0;

}

double sum_num(double z, double x)

{

return z + x;

}

A. Please enter two of your double numbers here 780.42

667.351

We have found the result of the two double numbers in the program to be: 1447.42

B. Please enter two of your double numbers here 65.452

879.45321

We have found the result of the two double numbers in the program to be: 944.90521

C. Please enter two of your double numbers here 129.743

568.0321

We have found the result of the two double numbers in the program to be: 686.7521

D. Please enter two of your double numbers here 78.12

9056.34

We have found the result of the two double numbers in the program to be: 9234.48

Answer – B. Please enter two of your double numbers here 65.452

879.45321

We have found the result of the two double numbers in the program to be: 944.90521


Frequently Asked Questions

Why do we use Double Data Type in C?

The double data type in the C language is responsible for storing very large numeric values, which the float (floating point) or integer data types aren’t able to store in any given program. The double datatype or double refers to that data type in the C language that helps in storing high-precision sorts of floating-point numbers or data in the computer memory. This data type is also known as double because it is capable of holding double the size of info and data as compared to the float.

Why do we use float if double is already capable of storing more data?

The double data type is comparatively larger in size since it holds more data. Thus, it makes a program slow. For faster compilation, we use the float data type. We only use double when the value given in the form of a variable is too large to be contained in float or int (integer data type).

What is the difference between the double data type and float data type?

The float data type is basically a single-precision sort of data type that is capable of holding 32 bits of decimal numbers or floating points. It equals a total of 4 bytes. The double data type is basically a precision sort of data type that is capable of holding 64 bits of decimal numbers or floating points. Thus, it is capable of storing exactly the double size of data as compared to the float data type.
Both of these are predefined in nature. It means that their names and meanings cannot be changed by a programmer throughout the program.
The float data type is much faster as compared to the double data type. It is mainly because of its small range in a program. It is capable of holding about 7 digits approximately, and the data type basically ranges from 1.5 x 10-45 to 3.4 x 1038. The double data type, on the other hand, is very big in size, thus very slow, and it can easily accommodate about 15 to 17 digits after or before a decimal point. The typical range of a double is about 5.0 x 10-345 to 1.7 x 10308.

Keep learning and stay tuned to get the latest updates on the GATE Exam along with Eligibility Criteria, GATE Syllabus for CSE (Computer Science Engineering), GATE CSE Notes, GATE CSE Question Paper, and more.

Also Explore,

Leave a Comment

Your Mobile number and Email id will not be published.

*

*