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: obtaining a c value from a perl script Date: Mon, 08 May 2017 11:39:30 -0700 Organization: None to speak of Lines: 30 Message-ID: References: <874lx6hqng.fsf@bsb.me.uk> <5205b36d-8bdb-c6c5-f410-9111da4ca1e1@verizon.net> <39983e3e-4cfb-4540-8cf6-28c17063ad84@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="6d7e0aa3e5b7ac222e8546bcb39ccb10"; logging-data="17144"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18aBmjnoz6FciVNQ1h9Hu6q" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:pSE7DdipwA1IqpZ27A+rrCCIE10= sha1:I66p+PB2grKHPRA9ZSh5zmBo28w= Xref: csiph.com comp.lang.c:109524 jameskuyper@verizon.net writes: > On Monday, May 8, 2017 at 12:27:01 PM UTC-4, Keith Thompson wrote: >> "James R. Kuyper" writes: > ... >> > What I think you want is >> > >> > if(sscanf(argv[1], "%f", &d) != 1) >> > { >> > // error handling >> > } >> >> That will let you detect something that's not a valid representation of >> a floating-point value, but it has undefined behavior on overflow. >> strtod() is trickier to use, but more robust. > > I agree that it's more robust, but for someone who could write > (double)argv[1] without realizing that it's nonsense, strtod() is > probably too tricky to use. Which is why I suggested atof() (while pointing out its weaknesses and suggesting strtod() as a more robust alternative). I suppose sscanf (with the right format string, "%lf") is more robust than atof(). -- 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"