我使用的是最新的海湾合作委员会与//需要将物品寄。为什么不 long double 工作?是的 printf 说明符 %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 ...
有帮助吗?

解决方案

除了错误的修饰符,Windows的哪个GCC港口? MINGW使用Microsoft C库,我似乎还记得该库不支持80bits Long Double(Microsoft C编译器使用64位长double,原因是各种原因)。

其他提示

从printf平:

l(嗯)下面的一个整数 转换对应于一个长int 或unsigned long int参数,或 以下n转换对应 指向一个长int参数,或 下文c 转换对应于一个wint_t参数或下s 转换对应的指针 wchar_t的论点。

我一下,A、e、E、f、F, g,或G转换对应一个 只要双参数。(C99允许 %LF,但SUSv2不。)

所以,你想要的 %Le ,不 %le

编辑:一些进一步的调查似乎表明,Mingw使用MSVC/win32运行时(用于这样的东西printf)-这地图长的双倍.所以混合的一个编译程序(如海湾合作委员会),提供了一个机长双与一个运行时,不似乎..是一个乱糟糟的。

是的 - 是 long double, ,您需要使用 %Lf (即,上案例“ L”)。

如果您使用的是mingw,则问题是默认情况下,mingw使用I/O resp。 Microsoft C运行时的格式化函数,该功能不支持80位浮点号(long double == double 在Microsoft Land)。

但是,MINGW还提供了一组替代实现 适当地支持长双打。要使用它们,请将函数名称前缀 __mingw_ (例如 __mingw_printf)。根据项目的性质,您可能还想在全球范围内 #define printf __mingw_printf 或使用 -D__USE_MINGW_ANSI_STDIO (这使所有版本都可以 printf- 家庭功能)。

在测试长双打的情况下,我遇到了一个修复程序!您必须使用-d__use_mingw_ansi_stdio编译项目:

Jason Huntley@centurian/home/developer/dependerencies/python-2.7.3/test $ gcc main.c

Jason Huntley@centurian/home/developer/depperencies/python-2.7.3/test $ a.exe c = 0.000000

Jason Huntley@centurian/home/developer/depperencies/python-2.7.3/test $ gcc main.c -d__use_mingw_ansi_stdio

Jason Huntley@centurian/home/developer/depperencies/python-2.7.3/test $ a.exe c = 42.000000

代码:

Jason Huntley@centurian /home/developer/dependencies/Python-2.7.3/test
$ cat main.c
#include <stdio.h>

int main(int argc, char **argv)
{
   long double c=42;

   c/3;

   printf("c=%Lf\n",c);

   return 0;
}

在C99中的长度修饰符 long double 似乎是 L 并不是 l. man fprintf (或Windows的同等用途)应告诉您特定平台。

正如其他答案中所说的那样,正确的转换说明符是 "%Lf".

您可能想通过使用 -Wformat (或者 -Wall, , 包括 -Wformat)在GCC调用中

$ gcc source.c
$ gcc -Wall source.c
source.c: In function `main`:
source.c:5: warning: format "%lf" expects type `double`, but argument 2 has type `long double`
source.c:5: warning: format "%le" expects type `double`, but argument 3 has type `long double`
$

C/C ++中的printf和Scanf函数使用Microsoft C库,此库不支持10个字节长double。因此,当您在C/C ++代码中使用printf和scanf函数以打印长时间的双重输出,并将一些输入作为长双重双重,它将始终给您错误的结果。

如果要使用长double,则必须使用“ __ging_printf”和“ __ging_mw_scanf”功能而不是printf和scanf。它支持10个字节长双重。

或者,您可以通过这样定义两个宏:“ #define printf __mingw_printf”和“ #define scanf __mingw_scanf”

使用标准格式进行长双重:%lf

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top