strtoul("-10", ...)
Neal M Gafter
gafter at m2.csc.ti.com
Wed Jul 25 07:39:28 AEST 1990
I have implemented the ANSI library function strtoul() [4.10.1.6] and
had a little problem interpreting the standard. To quote:
If the subject sequence begins with a minus sign, the value
resulting from the conversion is negated.
and
If the correct value is outside the range of representable
values, ULONG_MAX is returned, and the value of the macro
ERANGE is stored in errno.
My question is: what is the `range of representable values'? Unsigned
longs can represent values in the range [0..ULONG_MAX]. Alternately,
one might argue that any integer is representable as an unsigned long,
since unsigneds operate in modular arithmetic. This latter
possibility would imply that the macro errno is never set to ERANGE.
Discounting the possibility that X3J11 intended errno never to be set
to ERANGE by strtoul(), I assumed X3J11 meant the range
[0..ULONG_MAX]. I presumed the sentence about the minus sign was to
allow the string "-0" and to recognize (as errors) other negative
numbers. Thus, I assumed any string representing a negative number
should cause errno to be set to ERANGE and strtoul() to return
ULONG_MAX.
I talked to two different X3J11 committee members to confirm my
interpretation. To my surprise, I got two different answers, both
different from mine. One suggested the range [-ULONG_MAX..ULONG_MAX]
be allowed, the other suggested [LONG_MIN..ULONG_MAX], in both cases
with the returned result being computed using modular arithmetic.
Can the committee please create a definitive interpretation?
--
Arpa: gafter at csc.ti.com (Neal Gafter)
USnail: Texas Instruments MS 238, PO BOX 655474, Dallas, TX 75265
phone: (214) 995 - 0696 (office) or (214) 727 - 2082 (home)
--
Arpa: gafter at csc.ti.com (Neal Gafter)
USnail: Texas Instruments MS 238, PO BOX 655474, Dallas, TX 75265
phone: (214) 995 - 0696 (office) or (214) 727 - 2082 (home)
More information about the Comp.std.c
mailing list