Path: ...!news.mixmin.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!news.gegeweb.eu!gegeweb.org!nntp.terraraq.uk!.POSTED.tunnel.sfere.anjou.terraraq.org.uk!not-for-mail From: Richard Kettlewell Newsgroups: comp.lang.c Subject: Re: The difference between strtol() and strtoul() ? Date: Sun, 23 Jun 2024 17:39:37 +0100 Organization: terraraq NNTP server Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: innmantic.terraraq.uk; posting-host="tunnel.sfere.anjou.terraraq.org.uk:172.17.207.6"; logging-data="138659"; mail-complaints-to="usenet@innmantic.terraraq.uk" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux) Cancel-Lock: sha1:FfpSC4fEeRBUjigsUWX2sDMNcKQ= X-Face: h[Hh-7npe<v9!1Z&W?r\c.!4DXH5PWpga"ha +r0NzP?vnz:e/knOY)PI- X-Boydie: NO Bytes: 2149 Lines: 21 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/