DOUBLE TYPE IN C
High-precision floating-point data or numbers (up to 15 or 17 digits) can be stored using the double data type in the C programming language. Large decimal number values are stored there.
The stored numbers are twice as large as the maximum amount of data that may be stored in a float data type. It is called a double data type as a result.
In C, any variable declared with the double data type has a 64-bit (or 8-byte) size.
Note:
- In the double data format, the sign representation uses one bit, the exponent uses one bit, and the mantissa uses the remaining 52 bits.
- Between 1.7E-308 and 1.7E+308, the double in c can be found. Real numbers, decimals, negative values, and other types of data can all be represented by a double.
Double Variable Declaration Syntax:
double var_name;
The double keyword is used to declare the double variables in the language of C, followed by the variable name.
Double Variable Initialization:
For a variable to have a specific value, we must initialise the double variable with that specific value because when a variable is declared, it often has a garbage value.
In C, there are two methods for initializing a double:
Method 1- By utilizing the assignment operator to assign a value to the declared double variable:
double quantity;
quantity = 2.50;
Method 2- By using the assignment operator to the double variable during the declaration to assign it a value:
double quantity = 2.50;
Double example in C:
double temp_1 = 8.4;
double temp_2;
double temp_3;
temp_3 = 7.8;
In the aforementioned illustration, temp_1 is declared and given the value 8.4. Temp_2 is specified but not given a value, so it will have a trash value, and Temp3 is declared but not given a value, so it also has a garbage value but is later given the value 7.8.
Printing the Double value in C:
In C, a double can be printed using both %f and %lf. The format specifiers %f and %lf both stand for float and double, respectively. The printf() function in C respects double and float data equally.
#include <stdio.h>
int main()
{
double temp_1 = 8.4;
double temp_2;
double temp_3;
temp_3 = 7.8;
printf("Value of the temp_1 = %lf\n",temp_1);
printf("Value of the temp_2 = %f\n",temp_2);
printf("Value of the temp_3 = %f\n",temp_3);
}
OUTPUT:
![DOUBLE TYPE IN C](https://static.tutorialandexample.com/c/double-type-in-c1.png)
Double Representation in C:
- The way the double is implemented in the code determines how precise it is in C. The IEEE-754 standard is typically used by modern compilers.
- The Institute of Electrical and Electronics Engineers (IEEE) created the IEEE Standard for Floating-Point Arithmetic (IEEE 754) in 1985 as a technical standard for floating-point calculation.
- The standard addressed a number of issues that were present in the various floating-point implementations and restricted their portability and reliability.
The following is how 64-bit representation of numbers is done in such compilers:
- For the sign, the first bit is there.
- 52 bits of significand (mantissa) are used, with 0 denoting the positive and 1 the negative.
- The exponent is 11 bits.
- Up to 15 or 16 digits of value can be stored.
![DOUBLE TYPE IN C](https://static.tutorialandexample.com/c/double-type-in-c2.png)
Some Double Date Type Programs in C:
1. A Program to Convert Feet into Meters the Double Data Type is Used
#include <stdio.h>
// function
double feet_To_Meter (double feet)
{
return feet / 3.28;
}
int main()
{
double feet;
double meter;
printf ("Enter the value in feet: ");
scanf("%lf", &feet);
// Calling a function
meter = feet_To_Meter(feet);
printf (" The value in meter for the given feet is: %lf", meter);
return 0;
}
OUTPUT:
![DOUBLE TYPE IN C](https://static.tutorialandexample.com/c/double-type-in-c3.png)
Explanation:
The function 'function_To_Meter', which accepts a double value as an argument, has been defined in the code above. The feet value is converted into a meter and returned using the formula feet / 3.28.
2. Program to Convert an Integer Data Type in to a Double Data Type:
#include <stdio.h>
int main()
{
// declaration of variables of int data type
int numerator = 11, denominator = 4;
// declaration of variables of double data type
double remainder;
// calculation and storing the remainder
remainder = (double) numerator / denominator;
// printing output
printf ("Output: %lf \n", remainder);
}
OUTPUT:
![DOUBLE TYPE IN C](https://static.tutorialandexample.com/c/double-type-in-c4.png)
Explanation:
- The numerator and denominator in the example above are both of the integer type. The remaining variable of the double type has been created.
- Integer values will be output by the numerator and denominator as integer and integer, respectively.
- So, using the (double) numerator, we will change the numerator's data type to a double. The double value that is now returned by the numerator/denominator can be kept in the remainder.
3. Program to Convert Temperatures from Celsius to Fahrenheit:
#include <stdio.h>
int main()
{
// declaration of two double variables
double cel_temp, fahren_temp;
printf(" Celsius Temperature: ");
scanf (" %lf", &cel_temp);
// conversion of celsius to fahrenheit
fahren_temp = ( cel_temp * 1.8) + 32;
printf (" Fahrenheit Temperature is: %lf", fahren_temp);
return 0;
}
OUTPUT:
![DOUBLE TYPE IN C](https://static.tutorialandexample.com/c/double-type-in-c5.png)
Conclusion:
- In C, high-precision floating-point data or numbers are stored using the double.
- The double in C falls within the range of 5.0 10345 to 1.7 10308.
- The C double may store between 15 and 17 digits.
- The way the code is implemented determines the precise double precision.