Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to printf long double in C? [duplicate]

Tags:

c

double

[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

like image 567
elfsummer Avatar asked Jan 23 '15 23:01

elfsummer


People also ask

How do I print a long double in printf?

%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.

How do you print double double long?

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.

How do you use a long long double?

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.


1 Answers

The right format specifier for long double in printf() is "%Lf"

like image 181
Iharob Al Asimi Avatar answered Oct 20 '22 16:10

Iharob Al Asimi