English 中文(简体)
Conversion specifier of long double in C
原标题:
  • 时间:2009-11-19 15:59:06
  •  标签:
  • c
  • mingw

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
",d);
  printf("%.0LE
",d);
  printf("%.0Lf
",d);
  printf("%.0Lg
",d);
  printf("%.0LG
",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).

最佳回答

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
", (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
", 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

问题回答

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.

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:PracticeTeaErrmain.c||In function  main :|
D:PracticeTeaErrmain.c|7|warning: unknown conversion type character  L  in format [-Wformat=]|
D:PracticeTeaErrmain.c|7|warning: too many arguments for format [-Wformat-extra-args]|
D:PracticeTeaErrmain.c|7|warning: unknown conversion type character  L  in format [-Wformat=]|
D:PracticeTeaErrmain.c|7|warning: too many arguments for format [-Wformat-extra-args]|
D:PracticeTeaErrmain.c|8|warning: unknown conversion type character  L  in format [-Wformat=]|
D:PracticeTeaErrmain.c|8|warning: too many arguments for format [-Wformat-extra-args]|
D:PracticeTeaErrmain.c|8|warning: unknown conversion type character  L  in format [-Wformat=]|
D:PracticeTeaErrmain.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
",a);
    return 0;
}

Result Some Parameters on Install MinGW-W64





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签