Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: How does gcc (any compiler) actually convert a function call, say atoi(), into assembly/machine code? Date: Wed, 28 Dec 2016 18:40:29 -0800 Organization: None to speak of Lines: 38 Message-ID: References: <8760m3frtm.fsf@bsb.me.uk> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="e52b6227186de46e02b6ceefa726c58c"; logging-data="25079"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18NldenCrM06fV0uzR74dQw" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:bFrKcqoDLTDTldyn+RtriusX+W4= sha1:dfrnn1hFoOlhs3oJiLwpIWgEKjE= Xref: csiph.com comp.lang.c:100480 Ben Bacarisse writes: > Malcolm McLean writes: >> On Thursday, December 29, 2016 at 12:48:50 AM UTC, Tim Rentsch wrote: >>> I think you mean something different from what this says. >>> An actual atoi() function may be perfectly fine. What I >>> think you mean is that atoi() has a specification that is >>> lacking. Your complaint is not with how atoi() itself is >>> designed and/or implemented, which may be perfectly fine, >>> but with the specification for atoi() which allows lesser >>> quality implementations. >>> >> >> int x = atoi("NaN"); >> >> what should x be at this point and why? > > Zero. atoi must behave like (int)strtol(nptr, (char **)NULL, 10) except > on error, and there is no reason to take advantage of that permission to > return (or do) anything else in the this case (or, I'd argue, in any > other case). Whether there's any reason or not, the standard places no requirements on the behavior of atoi() on error. I'm reasonably sure that atoi() predates strtol(). There could still be old implementations that just read digits and build up the result, with no checks for numeric overflow. Some implementations of atoi() do just call strtol() and ignore errors -- but even then, if long int has a wider range than int, and strtol() returns a value outside the range of int, the conversion to int has undefined behavior. (Of course this doesn't apply to atoi("NaN").) -- 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"