How to predict size of outcome of vsprintf?

Sean McElroy sean at etnibsd.UUCP
Tue Mar 21 04:30:09 AEST 1989


In article <9872 at smoke.BRL.MIL> gwyn at brl.arpa (Doug Gwyn (VLD/VMB) <gwyn>) writes:
>No.  Sometimes you can accurately predict an upper bound on the output
>string length; for example if the format is "%d" and the numerical
>argument is known to lie between 0 and 5000, the maximum number of
>characters for the *sprintf() output will be 5 (including the null
>byte at the end).

If the format specification contains field width specifiers,
this will not work (e.g. sprintf ("%10d", 5000); uses 10 chars + 1
for terminator).  This implies that the format specifications need 
to be parsed in a manner similar to *sprintf in order to accurately
predict the number of chars in the result.  I was looking for something
which did nor require me to re-write *sprintf.  Also recall that
the original problem stated that the types of the input arguments
were unknown.  This implies that the type must be gleaned from
the format specification itself.  Although I'm no expert, I presume
that this is the mechanism *sprintf uses as well (e.g. %d means take
whatever the next input argument is and treat it as an int).

-- 
  ____,.,_..,__.,_.,__     Sean Philip McElroy
   __'..__._,_.__.__.__    Eaton Corp., SED
   _,___`_.'__.__.__.__    108 Cherry Hill Dr., Beverly, MA 01922
  ___`..'_,___.__.__,_     uunet!etnibsd!sean



More information about the Comp.lang.c mailing list