[Update] The results would still be the same no matter I use lf
, Lf
, llf
...I am using Code Blocks to compile and run my C program. I am using GNU GCC compiler.
I tried to printf long double float type on different computers including two Windows and one Mac but it turns out that none of them is working as I expected. The code is the following:
#include <stdio.h>
#include <stdlib.h>
int main()
{
double d = 6.232345235; //8 bytes
long double ld = 5.5767458458;
printf("%lf\n", ld);
return 0;
}
Either the return value is -0.000000 or another very huge negative number which I don't remember now.
Any help is greatly appreciated! :D
%Lf format specifier for long double %lf and %Lf plays different role in printf. So, we should use %Lf format specifier for printing a long double value.
We can print the double value using both %f and %lf format specifier because printf treats both float and double are same. So, we can use both %f and %lf to print a double value.
So when you are using printf and scanf function in your C/C++ code to print a long double as output and to take some input as a long double, it will always give you wrong result. If you want to use long double then you have to use " __mingw_printf " and " __mingw_scanf " function instead of printf and scanf.
The right format specifier for long double
in printf()
is "%Lf"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With