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

Categories

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

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

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...