strtoul("-10", ...)

Larry Jones scjones at thor.UUCP
Thu Jul 26 23:40:31 AEST 1990


In article <GAFTER.90Jul24163928 at m2.csc.ti.com>, gafter at m2.csc.ti.com (Neal M Gafter) writes:
> 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.
> 
> 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.

This was discussed at the last meeting.  The (unanimous, as I recall)
sense of the committee was that the intended meaning of the above is
that the digit sequence is to be converted to an unsigned long.  If
the value of the digit sequence is greater than ULONG_MAX, then the
unsigned long is set to ULONG_MAX and errno is set.  If there was a
leading minus sign, the unsigned long is negated according to the
rules for negation of unsigned values.  The resulting unsigned long
value is then returned.

I don't believe that this was a formal interpretation.  If you want
one, send a request to the X3 Secretariat, CBEMA.
----
Larry Jones                         UUCP: uunet!sdrc!thor!scjones
SDRC                                      scjones at thor.UUCP
2000 Eastman Dr.                    BIX:  ltl
Milford, OH  45150-2789             AT&T: (513) 576-2070
"This probably just goes to show something, but I sure don't know what."
-Calvin



More information about the Comp.std.c mailing list