Deutsch English Français Italiano |
<v51fgl$2j1fo$1@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Lew Pitcher <lew.pitcher@digitalfreehold.ca> Newsgroups: comp.lang.c Subject: Re: The difference between strtol() and strtoul() ? Date: Thu, 20 Jun 2024 14:48:53 -0000 (UTC) Organization: A noiseless patient Spider Lines: 54 Message-ID: <v51fgl$2j1fo$1@dont-email.me> References: <v51d1l$2fklr$1@news.xmission.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Injection-Date: Thu, 20 Jun 2024 16:48:54 +0200 (CEST) Injection-Info: dont-email.me; posting-host="b74a9812d57b461f2c77543759f00526"; logging-data="2721272"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/Ubzq9MNk62V8LMi5TRyBPuuXbrTxrj00=" User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508 git://git.gnome.org/pan2) Cancel-Lock: sha1:528GIxGF9iZyIJp8pSNDjte2vVg= Bytes: 3500 On Thu, 20 Jun 2024 14:06:45 +0000, Kenny McCormack wrote: > 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. IIUC, the ISO C standard does not make a distinction between strings that make sense for an unsigned long vs strings that make sense for a signed long. The standard says (with regards to the strtol, strtoll, strtoul, and strtoull functions): "... the expected form of the subject sequence is a sequence of letters and digits representing an integer with the radix specified by base, optionally preceded by a plus or minus sign ... . If the value of base is 16, the characters 0x or 0X may optionally precede the sequence of letters and digits, following the sign if present." so, it appears that the ISO C standard permits the input string to specify a sign, even if the resulting conversion does not. > 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). Why would you expect that? Again, the ISO standard says: "If the subject sequence has the expected form ... it is used as the base for conversion, ascribing to each letter its value ... . If the subject sequence begins with a minus sign, the value resulting from the conversion is negated (in the return type)." and "If the correct value is outside the range of representable values, LONG_MIN, LONG_MAX, LLONG_MIN, LLONG_MAX, ULONG_MAX, or ULLONG_MAX is returned (according to the return type and sign of the value, if any) ... ." > Comments? I find this all very counterintuitive. I can't comment on /your/ internalization of the standards and expected behaviour. But, the standard makes sense (in an eccentric sort of way) to me, in that the defining distinction of the various strto*l() functions is not the format of the input, but the format of the output of the function. > (*) Or should I say, "one would" ? > > P.S. Why isn't there a strtoi() or strtou() ? I know, of course, that > there is atoi(), but that doesn't have the error checking capability that > the strto* functions have. -- Lew Pitcher "In Skills We Trust"