C 中 long double 的转换说明符

标签 c mingw

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

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

相关文章:

c++ - 在循环中对 vector 进行排序

audio - SDL音频流回调函数从未触发

c - 变量的范围和生命周期

c - 带符号的补码算法

带哨兵的 C I/O 扫描和打印

C:输入if语句后如何自增

c - 从客户端到服务器建立 1000 个 TCP/IP 连接的延迟

c++ - 使用 MinGW 在 Windows 上的命令行编译 C++ 文件

c++ - 来自 Boost : cannot find the libraries to link with 的链接器错误

c++ - 带 MinGW 的互锁单链表 (SList)