printf和长双人
我正在使用Windows上的Netbeans使用最新的海湾合作委员会。为什么长double
不起作用? printf
specifier %lf
错误吗?
代码:
#include <stdio.h>
int main(void)
{
float aboat = 32000.0;
double abet = 5.32e-5;
long double dip = 5.32e-5;
printf("%f can be written %e\n", aboat, aboat);
printf("%f can be written %e\n", abet, abet);
printf("%lf can be written %le\n", dip, dip);
return 0;
}
输出:
32000.000000 can be written 3.200000e+004
0.000053 can be written 5.320000e-005
-1950228512509697500000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000000.000000
can be written 2.725000e+002
Press [Enter] to close the terminal ...
I am using the latest gcc with Netbeans on Windows. Why doesn't long double
work? Is the printf
specifier %lf
wrong?
Code:
#include <stdio.h>
int main(void)
{
float aboat = 32000.0;
double abet = 5.32e-5;
long double dip = 5.32e-5;
printf("%f can be written %e\n", aboat, aboat);
printf("%f can be written %e\n", abet, abet);
printf("%lf can be written %le\n", dip, dip);
return 0;
}
Output:
32000.000000 can be written 3.200000e+004
0.000053 can be written 5.320000e-005
-1950228512509697500000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000000.000000
can be written 2.725000e+002
Press [Enter] to close the terminal ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
是的 - 对于
长
,您需要使用%lf
(即,上case'l')。Yes -- for
long double
, you need to use%Lf
(i.e., upper-case 'L').从printf manpage:
和
因此,您想要
%le
,而不是%le
编辑:一些进一步的调查似乎表明mingw使用msvc/win32运行时(对于printf之类的东西) - 将长时间映射到两倍。因此,将编译器(如GCC)混合在一起,该编译器提供了一个本机长双重的运行时,似乎不是一团糟。
From the printf manpage:
and
So, you want
%Le
, not%le
Edit: Some further investigation seems to indicate that Mingw uses the MSVC/win32 runtime(for stuff like printf) - which maps long double to double. So mixing a compiler (like gcc) that provides a native long double with a runtime that does not seems to .. be a mess.
如果您使用的是mingw,则问题是默认情况下,mingw使用I/O resp。 Microsoft C运行时的格式化函数,该功能不支持80位浮点数(
Long double
==Double
Microsoft Land中)。但是,MINGW还提供了一套 DO 适当支持长双打的替代实现。要使用它们,请将函数名称与
__ mingw _
(例如__ mingw_printf
)前缀。根据项目的性质,您可能还需要全局#define printf __mingw_printf
或使用-D__USE_MINGW_ENSI_ANSI_STDIO
/代码> - 家庭功能)。If you are using MinGW, the problem is that by default, MinGW uses the I/O resp. formatting functions from the Microsoft C runtime, which doesn't support 80 bit floating point numbers (
long double
==double
in Microsoft land).However, MinGW also comes with a set of alternative implementations that do properly support long doubles. To use them, prefix the function names with
__mingw_
(e.g.__mingw_printf
). Depending on the nature of your project, you might also want to globally#define printf __mingw_printf
or use-D__USE_MINGW_ANSI_STDIO
(which enables the MinGW versions of all theprintf
-family functions).除了错误的修饰符(Windows的哪个GCC港口)之外? MINGW使用Microsoft C库,我似乎还记得该库不支持80bits long double(Microsoft C编译器使用64位长double,原因是各种原因)。
In addition to the wrong modifier, which port of gcc to Windows? mingw uses the Microsoft C library and I seem to remember that this library has no support for 80bits long double (microsoft C compiler use 64 bits long double for various reasons).
正在遇到这个问题,测试长双打,las,我遇到了一个修复程序!您必须使用-d__use_mingw_ansi_stdio编译项目:
代码:
Was having this issue testing long doubles, and alas, I came across a fix! You have to compile your project with -D__USE_MINGW_ANSI_STDIO:
Code:
在C99中,
的长度修饰符长
似乎是l
而不是l
。MAN FPRINTF
(或Windows的等效内容)应告诉您特定平台。In C99 the length modifier for
long double
seems to beL
and notl
.man fprintf
(or equivalent for windows) should tell you for your particular platform.如其他答案中所述,正确的转换说明符是
“%lf”
。您可能需要通过使用
-wformat
(或-wall
)打开格式警告,其中包括-wformat
)As has been said in other answers, the correct conversion specifier is
"%Lf"
.You might want to turn on the format warning by using
-Wformat
(or-Wall
, which includes-Wformat
) in the gcc invocationC/C ++中的printf和Scanf函数使用Microsoft C库,此库不支持10个字节长double。因此,当您在C/C ++代码中使用printf和scanf函数以打印长时间的双重输出,并将一些输入作为长双重双重,它将始终给您错误的结果。
如果要使用长double,则必须使用“ __GNGW_PRINTF”和“ __MingW_Scanf”功能而不是printf和scanf。它支持10个字节长双重。
或者,您可以定义两个宏:“ #define printf __mingw_printf”和“ #define scanf __mingw_scanf”
使用标准格式,用于长double:%lf
printf and scanf function in C/C++ uses Microsoft C library and this library has no support for 10 byte long double. So when you are using printf and scanf function in your C/C++ code to print a long double as output and to take some input as a long double, it will always give you wrong result.
If you want to use long double then you have to use " __mingw_printf " and " __mingw_scanf " function instead of printf and scanf. It has support for 10 byte long double.
Or you can define two macro like this : " #define printf __mingw_printf " and " #define scanf __mingw_scanf "
Use standard format for long double : %Lf