C++ “long double”的格式说明符是什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14871466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:46:14  来源:igfitidea点击:

What is format specifier for `long double`

c++c

提问by Prasad S Deshpande

I am working on application which works on various flavors of Unix and Windows 32bit and 64bit OS.

我正在开发适用于各种 Unix 和 Windows 32 位和 64 位操作系统的应用程序。

I am using long doubledata type, When I do sprintf()and used long doublewith %lfin it then it works fine with windows does not give any kind of error, however on Solaris platform it gives core dump.

我正在使用long double数据类型,当我sprintf()使用 它long double%lf在其中使用时,它在 Windows 上运行良好,不会出现任何类型的错误,但是在 Solaris 平台上它会提供核心转储。

Sample code for the same issue is as following.

相同问题的示例代码如下。

void main(){
    string size = "16622";
    string sizeFact = "20";
    long long sizeLongLong = strtoll(size);
    int factInt = atoi(sizeFact);
    long double sizeLongDouble = (long double) sizeLongLong/pow(2, factInt);
    char buf[512];
    sprintf(buf, "%.3lf %s", sizeLongDouble, "str");    
}

As mentioned above code works fine on windows 32bit and 64bit however for sprintf it gives me core on Solaris.

如上所述,代码在 Windows 32 位和 64 位上运行良好,但是对于 sprintf,它在 Solaris 上为我提供了核心。

I tried type casting in sprintf it worked fine.

我尝试在 sprintf 中进行类型转换,效果很好。

sprintf(buf, "%.3lf %s", (double) sizeLongDouble, "str");

What is the format specifier for long double?

什么是格式说明符long double

What is the mistake I am making here, am I using wrong format specifier because of which it is giving core?

我在这里犯了什么错误,我是否使用了错误的格式说明符,因为它给出了核心?

Why do I need to type cast one more time in sprintf()?

为什么我需要在 sprintf() 中再输入一次 cast?

采纳答案by Some programmer dude

For long doubleyou should use format "%Lf". Formatting with small L (i.e. "%lf") have no effect on POSIX systems (see the specification).

因为long double你应该使用 format "%Lf"。用小 L 格式化(即"%lf")对 POSIX 系统没有影响(请参阅规范)。

回答by unwind

The documentationmakes this pretty clear:

文档非常清楚地说明了这一点:

L

A following a, A, e, E, f, F, g, or Gconversion corresponds to a long double argument. (C99 > allows %LF, but SUSv2 does not.)

甲以下一个,A,E,E,F,F,G,或ģ转换对应于长double参数。(C99 > 允许 %LF,但 SUSv2 不允许。)

So, you should use %Lf.

所以,你应该使用%Lf.

回答by lhcopetti

I would add this as a comment but I cannot do that yet. It is also important to note that you could also use the Exponential 'p' notation for long doublein this way:

我会将此添加为评论,但我还不能这样做。同样重要的是要注意,您还可以long double以这种方式使用指数“p”表示法:

  • Fixed-point notation: %Lf
  • Exponential-notation: %Le
  • p notation: %La
  • 定点符号: %Lf
  • 指数符号: %Le
  • p符号: %La

回答by Ivaylo Strandjev

Depending on the compiler I have seen %Lfand %llftry both and see which works. The %lfthat you use is the format specifier for doublenot for long double. I believe on g++you should use %llf, while on visual studio it is %Lf.

根据我所见过的编译器%Lf%llf尝试两者,看看哪个有效。在%lf您使用是格式说明符double不适合long double。我相信g++你应该使用%llf,而在视觉工作室它是%Lf