Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: better atoi Date: Fri, 10 Feb 2017 15:16:25 -0800 Organization: None to speak of Lines: 35 Message-ID: References: <853138f9-54d1-435f-90d5-ef75a38e65bf@googlegroups.com> <1b1sv5k98w.fsf@pfeifferfamily.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="5b75f8dd226436b078478393b0f38f01"; logging-data="5703"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/1bewdBzlJGIakLkw0802u" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:1EyvcEQ9m9Eitm04Jam58XtlWtc= sha1:kbOUUOqvskcnWlrhXYOenpkzE2A= Xref: csiph.com comp.lang.c:103641 Joe Pfeiffer writes: > fir writes: >> http://www.cplusplus.com/reference/cstdlib/strtol/ >> >> it is said that strtol sets errno to ERANGE but is >> not mentioned with wrong value, so im not sure if >> it can be uniformly checked (probably yest though >> but only not noted (i not checked it though)) > > It says it returns LONG_MAX or LONG_MIN in this case (so it appears it > is possible that if the string does in fact represent one of those > values, you can't reliably detect the conversion was successful -- as > we've seen in other places, we can't rely on ERRNO being 0 on success. > We'd sort of hope for it in this function...). No, if you set errno to 0 before calling strtol() you can rely in it not being set to ERANGE unless there's an error. N1570 7.5p3: The value of errno may be set to nonzero by a library function call whether or not there is an error, provided the use of errno is not documented in the description of the function in this International Standard. Since the use of errno *is* documented for strtol(), it can't set it to nonzero unless there's an error. (I suppose you could interpret that wording in a way that allows to set errno even if it's successful, but that's not a reasonable reading.) -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"