Sujet : Re: The difference between strtol() and strtoul() ?
De : ldo (at) *nospam* nz.invalid (Lawrence D'Oliveiro)
Groupes : comp.lang.cDate : 24. Jun 2024, 01:48:12
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v5afob$j1nj$5@dont-email.me>
References : 1 2 3 4 5 6 7 8 9
User-Agent : Pan/0.158 (Avdiivka; )
On Sun, 23 Jun 2024 16:30:13 +0100, Ben Bacarisse wrote:
I think there /is/ something problematic with the wording about the
negation. It happens "in the return type" but how can
9223372036854775808 be negated in the type long long int? OK, the
negated value can be /represented/ in the type long long int but that's
not quite the same thing. On the othee hand, for the unsigned return
types, the negation "in the return type" is what produces ULONG_MAX for
"-1" when the negated value, -1, can't be /represented/ in the return
type. It's a case where, over the years, I've just got used to what's
happening.
In the C23 spec, section 7.24.1.7, “The strtol, strtoll, strtoul, and
strtoull functions”, paragraph 5 begins:
If the subject sequence has the expected form and the value of
base is zero, the sequence of characters starting with the first
digit is interpreted as an integer constant according to the rules
of 6.4.4.2.
Note this is excluding any sign. So if the non-negated value cannot be
represented in the desired type, then there is no valid value to apply
negation to, so according to paragraph 8, zero is returned.