[comp.std.c] strtoul question

ghoti+@andrew.cmu.edu (Adam Stoller) (05/25/90)

In looking over the specification for strtoul I find myself (as usual:-)
a bit confused.  According to the stanadard (which I've tried to trim to
the essentials and have added emphasis to):


> The strtoul function converts ... nptr to unsigned long int
> representation. First, it decomposes the input string into three parts:
>  ... a subject sequence RESEMBLING AN UNSIGNED INTEGER ... Then, it attempts to
> convert the subject sequence to an unsigned integer ...

> If the value of base is zero, the expected form of the subject sequence
> is that of an integer constant as described in 3.1.3.2, OPTIONALLY
> PRECEDED BY A PLUS OR MINUS SIGN, but not including an integer suffix.
> If the value of BASE is between 2 and 36, the expected form  ...
> OPTIONALLY PRECEDED BY A PLUS OR MINUS SIGN, but not including an
> integer suffix.  ...

> [...]

>  ...  IF THE SUBJECT SEQUENCE BEGINS WITH A MINUS SIGN,
>  THE VALUE RESULTING FROM THE CONVERSION IS NEGATED.  ...

> If the subject sequence is empty or does not have the expected form, no
> conversion is performed; the value of nptr  is stored in the object
> pointed to by endptr, provided that endptr is not a null pointer.

> The strtoul function returns the converted value, if any. If no
> conversion could be performed, zero is returned. 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.

What exactly (if one can use that term) is the strtoul() function
supposed to return when handed a string like "-987654" ?

    a) behave like labs() and return 987654
    b) return 0
    c) return ULONG_MAX
    d) set errno to ERANGE
    e) [b & d]
    f) [c & d]
    g) other - please explain.


Any help in interpreting this is much appreciated.

--fish

gwyn@smoke.BRL.MIL (Doug Gwyn) (05/25/90)

In article <UaLHQb200as9AS60EP@andrew.cmu.edu> ghoti+@andrew.cmu.edu (Adam Stoller) writes:
>What exactly (if one can use that term) is the strtoul() function
>supposed to return when handed a string like "-987654" ?

It's supposed to return ULONG_MAX + 1 - 987654.

As I mentioned in my posting about results of the NYC X3J11 meeting,
strtoul() overflow can occur only on conversion of the magnitude,
not on the negation phase of the conversion.