Re: strto[u]l and ERANGE
- From: "P.J. Plauger" <pjp@xxxxxxxxxxxxxx>
- Date: Wed, 6 Jul 2005 14:56:52 -0400
"Eric Sosman" <esosman@xxxxxxxxxxxxxxxxxxx> wrote in message
news:b6adnaHRifghulHfRVn-2Q@xxxxxxxxxxxxxx
> P.J. Plauger wrote:
>
>> "S.Tobias" <siXtY@xxxxxxxxxxxxxxxxxxxxxxxxxx> wrote ...
>>> [...] According to my reading of 7.20.1.4 (too long to quote)
>>>in `strtoul("-1", 0, 10)', first "1" is converted
>>>into a (mathematical) value `1'. Then, since there's
>>>a "-", the value is negated (value `-1'). Then a check is made
>>>if the value is representable (0..ULONG_MAX); it is clearly not,
>>>so strtoul should return ULONG_MAX and set `errno' to ERANGE.
>>>Have I got something wrong?
>>
>> Yep, the "it is clearly not" part. Negating an unsigned value
>> is well defined, since "unsigned" really means modulus arithmetic.
>
> Can we conclude that
>
> strtoul("99999999999999999999999999999999999999999"
> "99999999999999999999999999999999999999999"
> "99999999999999999999999999999999999999999"
> "99999999999999999999999999999999999999999"
> "99999999999999999999999999999999999999999"
> "99999999999999999999999999999999999999999",
> NULL, 0)
>
> ... is similarly well-defined, and should not report an out-of-
> range error?
>
> (I'm not being facetious; I'm looking for information.
> The Standard's text seems foggy about such matters.)
No. The converted value must be representable as a value
of the specified unsigned type. Negating a representable
value always yields a representable value; that was the
narrow point I was addressing.
P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
.
- Follow-Ups:
- Re: strto[u]l and ERANGE
- From: S.Tobias
- Re: strto[u]l and ERANGE
- References:
- strto[u]l and ERANGE
- From: Dave Vandervies
- Re: strto[u]l and ERANGE
- From: Peter Nilsson
- Re: strto[u]l and ERANGE
- From: S.Tobias
- Re: strto[u]l and ERANGE
- From: P.J. Plauger
- Re: strto[u]l and ERANGE
- From: Eric Sosman
- strto[u]l and ERANGE
- Prev by Date: Re: Funny old trick
- Next by Date: Re: how to compare
- Previous by thread: Re: strto[u]l and ERANGE
- Next by thread: Re: strto[u]l and ERANGE
- Index(es):
Relevant Pages
|