Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
579 views
in Technique[技术] by (71.8m points)

mingw - Conversion specifier of long double in C

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...