tags:

views:

258

answers:

2

The long double data type can have these conversion specifiers in C: %Le,%LE,%Lf,%Lg,%LG (reference).

I wrote a small program to test :

#include <stdio.h>
int main(void) {
  long double d = 656546.67894L;
  printf("%.0Le\n",d);
  printf("%.0LE\n",d);
  printf("%.0Lf\n",d);
  printf("%.0Lg\n",d);
  printf("%.0LG\n",d);
  return 0; 
}

Output:

-0

-4E-153

-0

-4e-153

-4E-153

But none is giving the desired output, which is 656547 (as you may easily understand). What is the reason?

The compiler used is gcc version 3.4.2 (mingw-special).

+7  A: 

From an old mingw wiki:

mingw uses the Microsoft C run-time libraries and their implementation of printf does not support the 'long double' type. As a work-around, you could cast to 'double' and pass that to printf instead. For example:

printf("value = %g\n", (double) my_long_double_value);

Note that a similar problem exists for 'long long' type. Use the 'I64' (eye sixty-four) length modifier instead of gcc's 'll' (ell ell). For example:

printf("value = %I64d\n", my_long_long_value);
Gonzalo
I know this,am not concerned with rounding off,but I want to know why the problem is for long double ?
nthrgeek
Ok. Updating my answer to give you what you want :-)
Gonzalo
Thanks,then it's mingw's fault !
nthrgeek
If the answer is good for you, you're supposed to accept it by clicking on the checkmark on the left. You can also 'upvote' it if you think there's any value in the answer.
Gonzalo
Well,I need 15 reputation to upvote :|
nthrgeek
That's fine. I just wanted to make sure you knew about accepting answers. Thanks!
Gonzalo
A: 

The MinGW C library is provided by MSVCRT.DLL, which is shipped with Windows and is in fact the old VC++ 6.0 library.

MinGW does however use the GNU C++ library, and although that relies on the underlying C library, it does support long double for output using iostreams. Even if you do not wish to use C++ generally, it may be worth using just enough to support this capability.

Clifford
No,the mingw special edition I am using does not support long double.I mean it's giving the same wrong result as before.
nthrgeek