strtoul question

Adam Stoller ghoti+ at andrew.cmu.edu
Fri May 25 23:46:15 AEST 1990


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



More information about the Comp.std.c mailing list