user173424
user173424

Reputation:

Formatting very large numbers in C

I have to output a large number, a double precision number using the following code:

fprintf(outFile,"           %11.0f   %d O(g(n))",factorialNotRecursive(index,factCount),factValue);

now the number gets so big that it jumps out of alignment further down the list of output. Once it gets past 11 digits, the max specified it continues to grow larger. Is there a way to cope with this? I'm not sure how big the inputs that will be run on this program.

Upvotes: 1

Views: 284

Answers (1)

pmg
pmg

Reputation: 108978

I think you cannot do it directly. You have to print to a string, then change the string.

/* pseudo (untested) code */

value = factorialNotRecursive(index, factCount);
/* make sure buff is large enough (or use snprintf if available) */
n = sprintf(buff, "%11.0f", value);
if (n > 11) {
    buff[10] = '+';
    buff[11] = 0;
}
fprintf(outFile,"           %s   %d O(g(n))", buff, factValue);

Upvotes: 1

Related Questions