什么是格式说明符`长double` [英] What is format specifier for `long double`

查看:186
本文介绍了什么是格式说明符`长double`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的应用程序,它在Unix和Windows的32位和64位操作系统的各种口味的作品。

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

我使用长双数据类型,当我做的sprintf()和使用长双%LF 在它,然后它正常工作与Windows不提供任何类型的错误,但在Solaris平台上它给核心转储

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

样code为同一问题如下。

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");    
}

如上code提到的在Windows 32位和64位的sprintf的它给我的核心在Solaris上工作正常不过。

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

我试过类型转换中的sprintf它工作得很好。

I tried type casting in sprintf it worked fine.

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

什么是格式说明长双

什么是我提出在这里,我使用错误的格式说明,因为其中它给内核错误了吗?

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

为什么我需要的sprintf到类型转换一次()?

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

推荐答案

有关长双你应该使用格式为:%LF。小L(即LF%)格式化对POSIX系统没有任何影响(见的the规范)。

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

这篇关于什么是格式说明符`长double`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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