Deutsch English Français Italiano |
<v54hc0$39bpi$1@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!feed.opticnetworks.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: James Kuyper <jameskuyper@alumni.caltech.edu> Newsgroups: comp.lang.c Subject: Re: The difference between strtol() and strtoul() ? Date: Fri, 21 Jun 2024 14:38:56 -0400 Organization: A noiseless patient Spider Lines: 61 Message-ID: <v54hc0$39bpi$1@dont-email.me> References: <v51d1l$2fklr$1@news.xmission.com> <v540t9$2gsdu$1@news.xmission.com> <20240621182839.00000dc4@yahoo.com> <20240621185314.00004fda@yahoo.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Injection-Date: Fri, 21 Jun 2024 20:38:59 +0200 (CEST) Injection-Info: dont-email.me; posting-host="1bfe3a7cbbbe82cd3f94aa227de7008f"; logging-data="3452722"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+tLJFg2yddOkywAJV83On4uRMwiY/6wBM=" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:ruKyrd4YWwd0nFiHpyIY1oME6PU= Content-Language: en-US In-Reply-To: <20240621185314.00004fda@yahoo.com> Bytes: 3833 On 6/21/24 11:53, Michael S wrote: > On Fri, 21 Jun 2024 18:28:39 +0300 > Michael S <already5chosen@yahoo.com> wrote: > >> On Fri, 21 Jun 2024 13:58:01 -0000 (UTC) >> gazelle@shell.xmission.com (Kenny McCormack) wrote: >>> >>> Yeah, now I get it. You really only need strtoimax() and >>> strtoumax(). >> >> Which are? uunfortunately, not part of C standard. They have been part of the C standard since C99. > BTW, I don't know what The Standard says about out-of-range inputs, but > at least https://en.cppreference.com/w/c/string/byte/strtol does not > say anything certain. especially about what stored in *str_end. "The strtoimax and strtoumax functions are equivalent to the strtol, strtoll, strtoul, and strtoull functions, except that the initial portion of the string is converted to intmax_t and uintmax_t representation, respectively." (7.8.2.3p2) You need to go to the descriptions of those other functions to get the detailed specifications. "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), and the value of the macro ERANGE is stored in errno." As I understand it, that means that if the input string represents a value outside of the range of representable values, then strtoimax() should return INTMAX_MIN or INTMAX_MAX, depending upon the sign, and strtouimax() should return UINTMAX_MAX. Both of them should store the value of ERANGE in errno, to distinguish these results from what you would get if the string happened to represent those values. The C standard uses end_ptr rather than str_end in it's description of these functions. "... First, they decompose the input string into three parts: an initial, possibly empty, sequence of white-space characters, a subject sequence resembling an integer represented in some radix determined by the value of base, and a final string of one or more unrecognized characters, including the terminating null character of the input string. ..." (7.21.4.7p2). That defines what the "final string" is. "If the subject sequence has the expected form, ... A pointer to the final string is stored in the object pointed to by endptr, provided that endptr is not a null pointer." (7.24.1.7p5). "If the subject sequence is empty or does not have the expected form ... the value of nptr is stored in the object pointed to by endptr, provided that endptr is not a null pointer." (7.21.4.7p7) That seems very precise and unambiguous to me, aside from what "the expected form" is, which is described elsewhere.