Deutsch English Français Italiano |
<v540t9$2gsdu$1@news.xmission.com> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!weretis.net!feeder9.news.weretis.net!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail From: gazelle@shell.xmission.com (Kenny McCormack) Newsgroups: comp.lang.c Subject: Re: The difference between strtol() and strtoul() ? Date: Fri, 21 Jun 2024 13:58:01 -0000 (UTC) Organization: The official candy of the new Millennium Message-ID: <v540t9$2gsdu$1@news.xmission.com> References: <v51d1l$2fklr$1@news.xmission.com> Injection-Date: Fri, 21 Jun 2024 13:58:01 -0000 (UTC) Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4"; logging-data="2650558"; mail-complaints-to="abuse@xmission.com" X-Newsreader: trn 4.0-test77 (Sep 1, 2010) Originator: gazelle@shell.xmission.com (Kenny McCormack) Bytes: 2844 Lines: 38 In article <v51d1l$2fklr$1@news.xmission.com>, Kenny McCormack <gazelle@shell.xmission.com> 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. There have been some useful responses on this thread, which is Good. Of course, there have also been the usual crappola-type responses, but one must learn to take the good with the bad. Anyway, I think the takeaway is that while it is what it is, an argument can certainly be made that it would have been better for the unsigned versions of these function to not accept signed input. If I were designing it, I would have had strtoul("-1") be a syntax error (not a C language syntax error - but a meta-language syntax error) - or, if not that, then have it return 1, not 2**N-1. But that's just me. I appreciate the responses indicating that it was probably done the way it was for actually both of these reasons: 1) Because it makes it more useful for C compiler writers - who were seen as the primary audience. 2) Because it means that the two functions are literally the same code. Both calculate the same bit pattern - the difference is only in the caller's interpretation of the result. >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. Yeah, now I get it. You really only need strtoimax() and strtoumax(). A result of any smaller type can be obtained by calling one of these functions and storing the result in an object of the smaller type. -- The randomly chosen signature file that would have appeared here is more than 4 lines long. As such, it violates one or more Usenet RFCs. In order to remain in compliance with said RFCs, the actual sig can be found at the following URL: http://user.xmission.com/~gazelle/Sigs/GodDelusion