Re: max size for printf() format conversion?
- From: Jens.Toerring@xxxxxxxxxxxxxxxxxxx
- Date: 31 May 2005 20:27:55 GMT
Matt Garman <fake@xxxxxxxxxxxxxx> wrote:
> Is there a clean, portable way to determine the maximum value of
> converted numerical fields with printf()-like functions? Doing this
> at compile-time would be preferable.
> For example, %i should never convert to a string that is longer than
> the number of digits in INT_MAX, right?
Two points: INT_MAX and INT_MIN could differ by more than one, so
to be on the save side you would have to take into account both if
you use the values of these limits. And for negative values you
would have to add one for the minus sign.
> I'd like to create a structure that contains character
> representations of numerical types (e.g., instead of storing 3421 as
> an int, it would store the char array "3421"). I'd like to make
> these fields a fixed-size static array, but obviously I don't want
> to make them too small.
If my calculations aren't wrong you should be on the safe side with
#defined MAX_INT_LENGTH ( ( sizeof( int ) * CHAR_BIT + 1 ) / 3 + 1 )
(both for signed and unsigned ints, you can drop the final "+ 1" for
unsigned ints). This can be evaluated at compile time and should be
portable.
Regards, Jens
--
\ Jens Thoms Toerring ___ Jens.Toerring@xxxxxxxxxxxxxxxxxxx
\__________________________ http://www.toerring.de
.
- References:
- max size for printf() format conversion?
- From: Matt Garman
- max size for printf() format conversion?
- Prev by Date: Re: message buffering for logs, sprintf, etc...
- Next by Date: Re: max size for printf() format conversion?
- Previous by thread: max size for printf() format conversion?
- Next by thread: Re: max size for printf() format conversion?
- Index(es):