sprintf 输出 float 和 double 变量时最多输出多少个字符?

发布于 2024-12-02 03:30:30 字数 226 浏览 2 评论 0原文

如果我这样做:

void printfloat(float number)
{
    printf("%f", number);
}

并且

void printdouble(double number)
{
    printf("%f", number);
}

每个函数可以输出的最大字符数是多少?

If I do this:

void printfloat(float number)
{
    printf("%f", number);
}

and

void printdouble(double number)
{
    printf("%f", number);
}

What is the maximum number of characters that can be output by each function?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

公布 2024-12-09 03:30:30

通过使用 MS Visual Studio 10 进行测试,得到的字符串为 811,

sprintf(buf, "%.*f", 500, -DBL_MAX);

当然更长的字符串可能具有更大的精度值。

但保留“%f”,“\0”的最大输出字符数为 317 + 1。
因此,对于可移植代码:

#include <float.h>
#include <stdio.h>

    char buf[1/*'-'*/ + (DBL_MAX_10_EXP+1)/*308+1 digits*/ + 1/*'.'*/ + 6/*Default? precision*/ + 1/*\0*/];
    sprintf(buf, "%f", -DBL_MAX);

函数 printfloat(float number) 唯一参数“number”是一个浮点型且仅限于浮点型范围,在传递给 sprintf() 时将转换为双精度型。因此它的最大值是FLT_MAX。因此,'\0' 的最大输出字符数为 47 + 1。

    char buf[1/*'-'*/ + (FLT_MAX_10_EXP+1)/*38+1 digits*/ + 1/*'.'*/ + 6/*Default? precision*/ + 1/*\0*/];
    sprintf(buf, "%f", -FLT_MAX);

Via testing, using MS Visual Studio 10, a string of 811 resulted from

sprintf(buf, "%.*f", 500, -DBL_MAX);

Certainly longer strings are possible with larger precision values.

But staying with "%f", the maximum number of characters output is 317 + 1 for the '\0'.
So for portable code:

#include <float.h>
#include <stdio.h>

    char buf[1/*'-'*/ + (DBL_MAX_10_EXP+1)/*308+1 digits*/ + 1/*'.'*/ + 6/*Default? precision*/ + 1/*\0*/];
    sprintf(buf, "%f", -DBL_MAX);

The function printfloat(float number) lone parameter "number", being a float and limited to a float's range, is converted to a double in passing to sprintf(). It's maximum value is thus FLT_MAX. So the maximum number of characters output is 47 + 1 for the '\0'.

    char buf[1/*'-'*/ + (FLT_MAX_10_EXP+1)/*38+1 digits*/ + 1/*'.'*/ + 6/*Default? precision*/ + 1/*\0*/];
    sprintf(buf, "%f", -FLT_MAX);
忆梦 2024-12-09 03:30:30

结论:

我无法让 snprintf 告诉我字符串有多大,并且我希望使代码尽可能独立于编译器。所以这是我想出的解决方案。

char numstr[50];
sprintf_s(numstr, "%g", *value);
m_stringRepresentation += numstr;

%g 以科学记数法输出数字,这严重限制了字符数。我选择了一个足够大的缓冲区来容纳可能输出的任何内容。我唯一的编译器依赖是 sprintf_s。

Conclusion:

I was unable to get snprintf to tell me how big the string would be, and I want to keep the code as compiler-independent as possible. So here is the solution I came up with.

char numstr[50];
sprintf_s(numstr, "%g", *value);
m_stringRepresentation += numstr;

%g outputs the number in scientific notation, which severely limits the number of characters. I picked a buffer large enough to contain anything that might output. My only compiler-dependency is on sprintf_s.

通过 wc -c 管道传输的快速测试程序显示浮点型有 47 个字符,双精度型有 317 个字符。程序:

#include <stdio.h>
#include <float.h>

int main(void) {
        printf("%f", -DBL_MAX);
}

请注意,您可以使用 snprintf 将输出限制为 n 个字符。

A quick test program piped through wc -c shows 47 characters for float, and 317 for double. The program:

#include <stdio.h>
#include <float.h>

int main(void) {
        printf("%f", -DBL_MAX);
}

Note that you can use snprintf to limit the output to n chars.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文