用C长双转换符 [英] Conversion specifier of long double in C

查看:82
本文介绍了用C长双转换符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

长双数据类型可以在这些C转换说明:%乐,LE%,LF%,%Lg都LG%(<一个href=\"http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=/com.ibm.xlcpp8a.doc/standlib/ref/printconversionspecifiers.htm\"相对=nofollow>引用)。

我写了一个小程序来测试:

 的#include&LT;&stdio.h中GT;
诠释主要(无效){
  长双D = 656546.67894L;
  的printf(%0Le \\ n,D);
  的printf(%。0LE \\ n,D);
  的printf(%0Lf \\ n,D);
  的printf(%0Lg \\ n,D);
  的printf(%。0LG \\ n,D);
  返回0;
}

输出:

-0

-4E-153

-0

-4E-153

-4E-153

但没有是给所需的输出,这是656547(因为你可能很容易理解)。原因是什么?

所使用的编译器是gcc版本3.4.2(MinGW的特)。


解决方案

从href=\"http://oldwiki.mingw.org/index.php/long%20double\" rel=\"nofollow\">老MinGW的一个


  

MinGW的使用Microsoft C运行时
  图书馆及其实施
  printf的不支持'长
  双型。作为变通,你
  可以转换为双并传递
  以代替了printf。例如:


 的printf(值=%G \\ N(双)my_long_double_value);


  

请注意,一个
  存在很久很久类似的问题
  类型。使用I64(眼64)
  长度修改,而不是GCC的LL
  (ELL ELL)。例如:


 的printf(值=%I64d号\\ n,my_long_long_value);

编辑(6年后):另请参见下面的基思·汤姆逊评论的解决办法

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

Edit (6 years later): Also see the comment below from Keith Thomson for a workaround.

这篇关于用C长双转换符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆