4

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

whacko__Cracko
  • 6,592
  • 8
  • 33
  • 35
  • They all seem to do what I expect. What is the desired output? – Aaron Nov 19 '09 at 16:02
  • You won't believe this, but failing to `#include ` is undefined behavior and the programs is free to do anything. Though, for most practical purposes, I'd wager the compiler slipping in the include for you and you really have something else in your _real_ code. – dirkgently Nov 19 '09 at 16:07
  • 2
    What does "%.0lf" do for double ?! For the given d I want the output to be: 656547 – whacko__Cracko Nov 19 '09 at 16:10
  • 1
    @dirkgently : You are not getting me,I am aware of the problems that may cause due to implicit declaration,I post only a part. – whacko__Cracko Nov 19 '09 at 16:12
  • @Gonzalo: I don't know why but I am not getting the output. – whacko__Cracko Nov 19 '09 at 16:13
  • What compiler/platform are you on? – Gonzalo Nov 19 '09 at 16:13
  • I am compiling it in gcc version 3.4.2 (mingw-special),I am aware of the fact that it is old,but don't think that's the problem. – whacko__Cracko Nov 19 '09 at 16:17
  • @nthrgeek - I think that is exactly the problem - MnGW 3.4.5 is printing garbage for me on this. My other compilers (MSVC, Digital Mars & Comeau) print what you expect. – Michael Burr Nov 19 '09 at 16:19
  • Yes I just now checked with in MSVC,it's working fine there.But why in GCC ? I don't think it is implementation defined.A GCC bug ? – whacko__Cracko Nov 19 '09 at 16:23
  • Can you please update your question with the desired results? We know how you initialized 'd', we need to know what it printed. – Tim Post Nov 19 '09 at 16:30
  • Funny - the question includes , so that requires a C++ compiler, not a C compiler? And the tag is for the wrong language? FWIW: G++ 4.0.1 on MacOS X gives the outputs 7e+05 7E+05 656547 7e+05 7E+05 – Jonathan Leffler Nov 21 '09 at 07:39

3 Answers3

11

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);

Edit (6 years later): Also see the comment below from Keith Thompson for a workaround:

#define __USE_MINGW_ANSI_STDIO 1 in the source file or change the command line to gcc -D__USE_MINGW_ANSI_STDIO=1

Gonzalo
  • 20,805
  • 3
  • 75
  • 78
  • 1
    I know this,am not concerned with rounding off,but I want to know why the problem is for long double ? – whacko__Cracko Nov 19 '09 at 16:20
  • Ok. Updating my answer to give you what you want :-) – Gonzalo Nov 19 '09 at 16:26
  • 2
    Thanks,then it's mingw's fault ! – whacko__Cracko Nov 19 '09 at 16:33
  • 2
    There's a workaround: `#define __USE_MINGW_ANSI_STDIO 1` in the source file or change the command line to `gcc -D__USE_MINGW_ANSI_STDIO=1`. – Keith Thompson Jul 28 '15 at 18:49
  • The link to the old MinGW wiki is broken, and the relevant page doesn't appear to exist any more, so would someone please edit in an archive link when the edit queues are clear? https://web.archive.org/web/20090919052257/http://oldwiki.mingw.org/index.php/long%20double is the only valid archive URL I could find. – AJM Feb 17 '23 at 10:25
1

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
  • 88,407
  • 13
  • 85
  • 165
0

using MinGW-W64 with architecture i686(x86-64 can't work) maybe a solution. I try it and it works.

These are compile info:

||=== Build: Debug in TeaErr (compiler: GNU GCC Compiler) ===|
D:\Practice\TeaErr\main.c||In function 'main':|
D:\Practice\TeaErr\main.c|7|warning: unknown conversion type character 'L' in format [-Wformat=]|
D:\Practice\TeaErr\main.c|7|warning: too many arguments for format [-Wformat-extra-args]|
D:\Practice\TeaErr\main.c|7|warning: unknown conversion type character 'L' in format [-Wformat=]|
D:\Practice\TeaErr\main.c|7|warning: too many arguments for format [-Wformat-extra-args]|
D:\Practice\TeaErr\main.c|8|warning: unknown conversion type character 'L' in format [-Wformat=]|
D:\Practice\TeaErr\main.c|8|warning: too many arguments for format [-Wformat-extra-args]|
D:\Practice\TeaErr\main.c|8|warning: unknown conversion type character 'L' in format [-Wformat=]|
D:\Practice\TeaErr\main.c|8|warning: too many arguments for format [-Wformat-extra-args]|
||=== Build finished: 0 error(s), 8 warning(s) (0 minute(s), 1 second(s)) ===|

These are my code:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    long double a;
    scanf("%Lf",&a);
    printf("%Lf\n",a);
    return 0;
}

Result Some Parameters on Install MinGW-W64

DanielM
  • 3,598
  • 5
  • 37
  • 53
Zane
  • 1
  • 1