Sujet : Re: The difference between strtol() and strtoul() ?
De : invalid (at) *nospam* invalid.invalid (Richard Kettlewell)
Groupes : comp.lang.cDate : 23. Jun 2024, 17:39:37
Autres entêtes
Organisation : terraraq NNTP server
Message-ID : <wwvsex3aa0m.fsf@LkoBDZeT.terraraq.uk>
References : 1
User-Agent : Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux)
gazelle@shell.xmission.com (Kenny McCormack) writes
Interestingly, I note that strtoul() accepts strings that begin with a
sign (+ or -). This is odd, since you'd (*) think that a sign
(particularly, a minus) would be a syntax error in parsing for an
unsigned value.
>
Further, although the (Linux) man page is more than a bit murky on the
subject, it seems that the result of parsing, say, "-1", with
strtoul() is the largest unsigned value (usually, 2**N-1 or a lot of
F's (in hex)). Whereas, I would expect it to be 1 (i.e., just take
the absolute value).
>
Comments? I find this all very counterintuitive.
I can think of contexts where the string -1 would be read as meaning 1
(e.g. GF(2^n)) but I don’t think most people would think they were a
sensible analogy for stroul behavior. Its behavior seems consistent with
the normal meaning of unary minus (i.e. additive inverse) and of course
with C’s treatment of unsigned integer types.
-- https://www.greenend.org.uk/rjk/