long double 数据类型在 C 中可以具有以下转换说明符:%Le、%LE、%Lf、%Lg、%LG ( reference )。
我写了一个小程序来测试:
#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;
}
输出:
-0
-4E-153
-0
-4e-153
-4E-153
但是没有一个给出所需的输出,即 656547(正如您可能很容易理解的那样)。原因是什么?
使用的编译器是gcc版本3.4.2(mingw-special)。
最佳答案
来自 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);
编辑(6 年后):另请参阅 Keith Thompson 的以下评论以获取解决方法:
#define __USE_MINGW_ANSI_STDIO 1
in the source file or change the command line togcc -D__USE_MINGW_ANSI_STDIO=1
关于C 中 long double 的转换说明符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24113989/