Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.earthlink.com!news.earthlink.com.POSTED!not-for-mail NNTP-Posting-Date: Wed, 01 Aug 2012 07:18:01 -0500 Message-ID: <50191E78.CDB@mindspring.com> Date: Wed, 01 Aug 2012 08:18:00 -0400 From: pete Reply-To: pfiland@mindspring.com Organization: PF X-Mailer: Mozilla 3.04Gold (WinNT; I) MIME-Version: 1.0 Newsgroups: comp.lang.c Subject: Re: Restricted unsigned integer range would have been better? References: <5011E247.1F7B@mindspring.com> <5012954D.20C@mindspring.com> <5018AE22.152C@mindspring.com> <0.41a5266becbe1d48f128.20120801125152BST.87r4rq4yp3.fsf@bsb.me.uk> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Lines: 79 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 4.154.219.167 X-Trace: sv3-F0L2pCXwsRC7IpGKXA5FkOSs6CqqOIBrncIPqOruY60ejS4raFIKwXy3dZei7SvBvpt0NGlg7xjxOti!F9m77IeptYwjZ9Fp1jq3o7IVR5fYUXD86+z6HfngkuvK9qH3WCL6jzR9QCPFD7pVQK6w+6Fl+8L7!oZYSpb96EQ== X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 3440 Xref: csiph.com comp.lang.c:24048 Ben Bacarisse wrote: > > David Brown writes: > > > On 01/08/2012 06:18, pete wrote: > >> Philip Lantz wrote: > >>> > >>> David Brown wrote: > >>>> Les Cargill wrote: > >>>>> Keith Thompson wrote: > >>>>>> Les Cargill writes: > >>>>>>> Type conversions must be made explicit. > >>>>>> > >>>>>> I presume that's meant as advice, not a language rule. > >>>>> > >>>>> Sorry - yes, it's advice. > >>>> > >>>> Good advice. > >>>> > >>>> For my work, it is more than advice - it is a style rule, > >>>> and I enforce > >>>> it with compiler warnings (-Wconversion -Wsign-conversion in gcc). > >>> > >>> Do you really? Would you use casts in situations like these? > >>> 1. char c = 'x'; > >>> 2. int *p = malloc(N * sizeof *p); > >>> 3. free(p); > >>> 4. printf("%c\n", c); > >>> 5. while (*s != '\0') > >>> n = n * 10 + (*s - '0'); > >>> > >>> I would find them to be undesirable clutter. > >> > >> I was thinking the same thing. > >> > >> And I think you meant to imply > >> that (*s) is an expression of type char. > >> > >> If n is of type int and also not negative, > >> then you could write without having to use parentheses > >> n = n * 10 - '0' + *s; > >> with no possibility of overflow. > > I don't understand that. Did you mean "and also unsigned" rather than > "and also not negative"? No. It reminded me of a line from a toy atoi that I once wrote as an exercise. int a_toi(const char *nptr) { int n; n = 0; while (isspace(*nptr)) { ++nptr; } if (*nptr != '-') { if (*nptr == '+') { ++nptr; } while (isdigit(*nptr)) { n = 10 * n - '0' + *nptr++; } } else { ++nptr; while (isdigit(*nptr)) { n = 10 * n + '0' - *nptr++; } } return n; } -- pete