C double Data Type
In C, the double data type is used to store floating-point numbers with double precision. It allows storing real numbers with higher precision compared to the float data type and is commonly used for mathematical computations that require greater accuracy.
1 Storage Size of double Data Type
The storage size of the double data type is typically 8 bytes (64 bits), though it can vary based on the system and compiler.
| Type | Storage Size |
|---|---|
double | 8 bytes |
2 Values Stored by double Data Type
The double data type stores floating-point numbers (numbers with decimal points) with double precision. It provides greater accuracy and a larger range compared to float.
Example values that can be stored in double:
3.14, -0.0005, 12345.6789, -987654.321
3 Example: Declaring and Using double Variables
Let’s see a simple program demonstrating how to declare and use double variables in C.
main.c
#include <stdio.h>
int main() {
double num1 = 3.1415926535;
double num2 = -98.7654;
double sum = num1 + num2;
printf("Number 1: %lf\n", num1);
printf("Number 2: %lf\n", num2);
printf("Sum: %lf\n", sum);
return 0;
}
Explanation:
- We declare three
doublevariables:num1,num2, andsum. - We assign values 3.1415926535 and -98.7654 to
num1andnum2respectively. - The sum of these two numbers is stored in the
sumvariable. - We print the values of
num1,num2, andsumusingprintf()with%lfformat specifier.
Output:
Number 1: 3.141593
Number 2: -98.765400
Sum: -95.623807
4 Checking Storage Size of double Programmatically
We can determine the storage size of double in bytes using the sizeof operator.
main.c
#include <stdio.h>
int main() {
printf("Size of double: %lu bytes\n", sizeof(double));
return 0;
}
Output (varies based on system architecture):
Size of double: 8 bytes
5 Minimum and Maximum Values of double
The range of values a double can store depends on the system and the floating-point representation used. Typically, for an 8-byte double:
| Storage Size | Minimum Value | Maximum Value |
|---|---|---|
| 8 bytes | ≈ -1.7 × 10308 | ≈ 1.7 × 10308 |
6 Getting Maximum and Minimum Values of double Programmatically
The maximum and minimum values of a double can be retrieved using float.h.
main.c
#include <stdio.h>
#include <float.h>
int main() {
printf("Minimum double value: %e\n", DBL_MIN);
printf("Maximum double value: %e\n", DBL_MAX);
return 0;
}
Output:
Minimum double value: 2.225074e-308
Maximum double value: 1.797693e+308
Conclusion
In this tutorial, we explored the double data type in C, including:
- Its ability to store floating-point numbers with higher precision.
- Its typical storage size of 8 bytes.
- How to get the storage size programmatically using
sizeof(). - The minimum and maximum values it can store.
- How to retrieve these values using
float.h.
The double data type is widely used for precise calculations in scientific and engineering applications.
