4

When I run the Ansi C program below, a value of "0.000000" is printed out. Does anyone know why the value "561.308000" is not being printed out? I am using Dev-C++ to run the program, and the compiler I am using is: Mingw port of GCC (GNU Compiler Collection), version MSVCRT 2.95.2-1.

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

main()
{
    long double x = 561.308;
    printf("%Lf",x);
}
Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
  • 1
    It works well on [Wandbox](http://melpon.org/wandbox/permlink/QDKLzZcAw0x57l4B). What is your compiler? – MikeCAT Sep 21 '15 at 03:32
  • it works good in [ideone](http://ideone.com/WiZu2D). – Rafaf Tahsin Sep 21 '15 at 03:33
  • I am using Dev-C++5.11. – Spacedog789 Sep 21 '15 at 03:33
  • Dev-C++ is an editor. What is your **compiler**? Please also include the version of your compiler. – MikeCAT Sep 21 '15 at 03:34
  • I use Mingw port of GCC (GNU Compiler Collection), version MSVCRT 2.95.2-1, as my compiler. – Spacedog789 Sep 21 '15 at 03:36
  • 1
    `-0.000000` is printed on Dev-C++ 5.11 and gcc (GCC) 4.8.1 – MikeCAT Sep 21 '15 at 03:36
  • 1
    This is implementation specific behavior. Saying you're using GCC isn't saying what version of GCC, but as MikeCAT noticed, at least we know 4.8.1 is showing -0.000.... There are a huge range of potential issues. Microsoft's C runtime doesn't support 80 bit floating point numbers (haven't checked very recent versions though), which means they map long double to double sometimes. The long double specifier should not be considered very portable because of this. __mingw_printf is a version that might help – JVene Sep 21 '15 at 03:39
  • 1
    A better way of expressing what @JVene seems to be saying is that your compiler and libc ("C runtime") probably disagree on what `long double` is. mingw is a mess like this... – R.. GitHub STOP HELPING ICE Sep 21 '15 at 04:25
  • Using `printf("%Le",x);` or `printf("%La",x);` would provide more info. – chux - Reinstate Monica Sep 21 '15 at 04:28
  • `561.308` is double. To get long double use [`561.308L`](http://stackoverflow.com/q/21557816/995714) – phuclv Sep 21 '15 at 05:22
  • long double in gcc is 80-bit extended precision type, but mingw uses MSVC's CRT library which doesn't support that type. So you must use mingw64 or some other workarounds – phuclv Sep 21 '15 at 05:26
  • @LưuVĩnhPhúc Mingw64 won't solve it, same issue. – Lundin Sep 21 '15 at 07:06
  • The fix is available [here](http://stackoverflow.com/q/28523892/995714) – phuclv Sep 21 '15 at 07:51

2 Answers2

1

Compiling with gcc (GCC) 4.8.1 on Windows 7 64bit with this command is worked. (Add -std=c99 to the option to have the compiler work with C99)

gcc -Wall -Wextra -std=c99 test.c -o test.exe

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
0

the zero might be the return value of the main() it is printed Could you try :

printf("Answer is : %Lf",x);

to see whether you got 0.000000 or Answer is: 0.0000

And how about

printf("Answer is : %Lf", 561.38888);

I have this:

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

int main(void)
{
    long double x = 561.308;
    printf("%Lf",x);
   return 0;
}
AlbertFG
  • 157
  • 13