gpt4 book ai didi

C 中 long double 的转换说明符

转载 作者:行者123 更新时间:2023-11-30 17:32:32 24 4
gpt4 key购买 nike

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-timelibraries and their implementation ofprintf does not support the 'longdouble' type. As a work-around, youcould cast to 'double' and pass thatto printf instead. For example:

printf("value = %g\n", (double) my_long_double_value);

Note that asimilar 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 to gcc -D__USE_MINGW_ANSI_STDIO=1

关于C 中 long double 的转换说明符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24113989/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com