7

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?

Stephen Canon
  • 103,815
  • 19
  • 183
  • 269
Almo
  • 15,538
  • 13
  • 67
  • 95
  • You can always control that amount: http://www.lix.polytechnique.fr/~liberti/public/computing/prog/c/C/FUNCTIONS/format.html#width – BlackBear Aug 29 '11 at 20:22
  • 1
    I don't want to control it. I want to print everything it will ordinarily print, and know what the max is for that. – Almo Aug 29 '11 at 20:31
  • 2
    Part of this is already answered. At least for doubles:http://stackoverflow.com/questions/1701055/what-is-the-maximum-length-in-chars-needed-to-represent-any-double-value – Chase Henslee Aug 29 '11 at 20:37
  • Awesome. I searched before posting, and that one didn't come up. It didn't come up from the pre-question search, either. – Almo Aug 29 '11 at 20:40
  • 1
    @Doug T.: `printf` does not take a pointer, it takes the float value itself; the `%lf` specifier is only needed for `scanf` -- it has no meaning for `printf` (§7.20.6.1 paragraph 7: "The length modifiers and their meanings are ... `l` ... has no effect on a following a, A, e, E, f, F, g, or G conversion specifier.") – Stephen Canon Aug 29 '11 at 20:49
  • @Doug T. Adding to Stephen Canon's comment, printf will upconvert all float arguments to double before printing them. – Praetorian Aug 29 '11 at 20:53

3 Answers3

6

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);
chux
  • 61
  • 1
  • 1
2

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.

Almo
  • 15,538
  • 13
  • 67
  • 95
1

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.

Kevin
  • 53,822
  • 15
  • 101
  • 132
  • Or use `snprintf(NULL, 0, ...);` to find out how many characters will be needed for the buffer so you can allocate that many. – Chris Lutz Aug 29 '11 at 20:46
  • I will probably do the snprintf solution in this comment. From my other reading on the subject, I'm not 100% convinced 317 is the max. I'm mostly convinced, but not enough to publish mission-critical code with it. :) – Almo Aug 29 '11 at 20:49
  • snprintf isn't defined in our codebase. I tried #include "stdio.h" with no luck. – Almo Aug 29 '11 at 20:54
  • @Almo If you're using MSVC try using `_snprintf` – Praetorian Aug 29 '11 at 20:56
  • _snprintf_s just tells me that the buffer I give it is too small. If I need to know how big the buffer has to be for it to tell me how big a buffer I need, I fail to see the point. – Almo Aug 30 '11 at 13:27
  • Looking at another SO discussion (http://stackoverflow.com/questions/2915672/snprintf-and-visual-studio-2010) and the links therein, it is clear that MS doesn't care about such trivialities as standards or convenience. MS says they're focusing on the C++ parts, so it looks like you have a couple options: use %g, use a big buffer (1080? from above), or use c++ (std::string, I believe). – Kevin Aug 30 '11 at 14:26