Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
302 views
in Technique[技术] by (71.8m points)

c++ - What is format specifier for `long double`

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

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.

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.

I tried type casting in sprintf it worked fine.

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

What is the format specifier for 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()?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...