C long double Data Type
In C, the long double data type is used to store floating-point numbers with higher precision than float or double.
long double is typically used when more decimal places are required for calculations involving scientific computations, financial calculations, and other precision-sensitive tasks.
1 Storage Size of long double Data Type
The storage size of the long double data type depends on the system and compiler being used. Typically:
| System / Compiler | long double Size |
|---|---|
| 32-bit Windows (MSVC) | 8 bytes (same as double) |
| 64-bit Windows (MSVC – x86_64) | 8 bytes (same as double) |
| 32-bit Linux (GCC/Clang – x86) | 12 bytes (80-bit extended precision, padded to 12 or 16 bytes) |
| 64-bit Linux/macOS (GCC/Clang – x86_64, LP64) | 16 bytes (128-bit precision on some systems) |
2 Values Stored by long double Data Type
The long double data type stores floating-point numbers, including:
3.141592653589793238L, -2.718281828459045L, 1.234567890123456789L
It provides a higher degree of precision compared to double and float.
3 Example: Declaring and Using long double Variables
Let’s see a simple program demonstrating how to declare and use long double variables in C.
main.c
#include <stdio.h>
int main() {
long double num1 = 3.141592653589793238L;
long double num2 = -2.718281828459045L;
printf("Value of num1: %.18Lf\n", num1);
printf("Value of num2: %.18Lf\n", num2);
return 0;
}
Explanation:
- We declare two
long doublevariablesnum1andnum2. - The suffix
Lensures that the values are treated aslong double. - We use
%.18Lfinprintf()to display 18 decimal places.
Output:
Value of num1: 3.141592653589793238
Value of num2: -2.718281828459045000
4 Checking Storage Size of long double Programmatically
We can determine the storage size of long double in bytes using the sizeof operator.
main.c
#include <stdio.h>
int main() {
printf("Size of long double: %lu bytes\n", sizeof(long double));
return 0;
}
Output (varies based on system architecture):
Size of long double: 16 bytes
5 Minimum and Maximum Values of long double
The range of values a long double can store depends on its size:
| Storage Size | Minimum Value | Maximum Value |
|---|---|---|
| 8 bytes | ±1.2E-308 | ±1.8E+308 |
| 16 bytes | ±3.4E-4932 | ±1.1E+4932 |
6 Getting Maximum and Minimum Values of long double Programmatically
The maximum and minimum values of a long double can be retrieved using float.h.
main.c
#include <stdio.h>
#include <float.h>
int main() {
printf("Minimum long double value: %Le\n", LDBL_MIN);
printf("Maximum long double value: %Le\n", LDBL_MAX);
return 0;
}
Output:
Minimum long double value: 3.362103e-4932
Maximum long double value: 1.189731e+4932
Conclusion
In this tutorial, we explored the long double data type in C, including:
- Its ability to store high-precision floating-point numbers.
- Its typical storage size of 8, 10, 12, or 16 bytes, depending on the system.
- How to determine the storage size programmatically using
sizeof(). - The minimum and maximum values it can store.
- How to retrieve these values using
float.h.
