printf and long double
From the printf manpage:
l (ell) A following integer conversion corresponds to a long int or unsigned long int argument, or a following n conversion corresponds to a pointer to a long int argument, or a following c conversion corresponds to a wint_t argument, or a following s conversion corresponds to a pointer to wchar_t argument.
and
L A following a, A, e, E, f, F, g, or G conversion corresponds to a long double argument. (C99 allows %LF, but SUSv2 does not.)
So, you want %Le
, not %le
Edit: Some further investigation seems to indicate that Mingw uses the MSVC/win32 runtime(for stuff like printf) - which maps long double to double. So mixing a compiler (like gcc) that provides a native long double with a runtime that does not seems to .. be a mess.
Yes -- for long double
, you need to use %Lf
(i.e., upper-case 'L').
If you are using MinGW, the problem is that by default, MinGW uses the I/O resp. formatting functions from the Microsoft C runtime, which doesn't support 80 bit floating point numbers (long double
== double
in Microsoft land).
However, MinGW also comes with a set of alternative implementations that do properly support long doubles. To use them, prefix the function names with __mingw_
(e.g. __mingw_printf
). Depending on the nature of your project, you might also want to globally #define printf __mingw_printf
or use -D__USE_MINGW_ANSI_STDIO
(which enables the MinGW versions of all the printf
-family functions).
In addition to the wrong modifier, which port of gcc to Windows? mingw uses the Microsoft C library and I seem to remember that this library has no support for 80bits long double (microsoft C compiler use 64 bits long double for various reasons).
Was having this issue testing long doubles, and alas, I came across a fix! You have to compile your project with -D__USE_MINGW_ANSI_STDIO:
Jason Huntley@centurian /home/developer/dependencies/Python-2.7.3/test $ gcc main.c
Jason Huntley@centurian /home/developer/dependencies/Python-2.7.3/test $ a.exe c=0.000000
Jason Huntley@centurian /home/developer/dependencies/Python-2.7.3/test $ gcc main.c -D__USE_MINGW_ANSI_STDIO
Jason Huntley@centurian /home/developer/dependencies/Python-2.7.3/test $ a.exe c=42.000000
Code:
Jason Huntley@centurian /home/developer/dependencies/Python-2.7.3/test
$ cat main.c
#include <stdio.h>
int main(int argc, char **argv)
{
long double c=42;
c/3;
printf("c=%Lf\n",c);
return 0;
}