Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: C idioms Date: Thu, 11 Oct 2018 10:38:23 -0700 Organization: None to speak of Lines: 53 Message-ID: References: <36d546c5-b471-45fc-b87a-8ebebdc0c0b7@googlegroups.com> <87pnwlhp1q.fsf@bsb.me.uk> <54e12132-07e7-432f-95e8-f292f5f5bdad@googlegroups.com> <87y3b6u3ev.fsf@bsb.me.uk> <877eiptkjj.fsf@bsb.me.uk> <87zhvksdz6.fsf@bsb.me.uk> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="2e98a4ae6d37959bf94050b86ea67cbf"; logging-data="20591"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+Ie26/utvqQ4NdVOkIpAIB" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:sAILKdwHLdLO65UTkoJ7EwU1Js0= sha1:Q6NeUUPuZuTEYTxlEDWrCUvTACw= Xref: csiph.com comp.lang.c:136668 scott@slp53.sl.home (Scott Lurndal) writes: > Ben Bacarisse writes: >>Bart writes: > >>> int main(void) { >>> FILE *f; >>> char *infile = "/mx/mm.pro"; > > Bad programmer! Pass it in to main as an argument. It depends. If it's passed as an argument, then whoever runs the program has to know that "/mx/mm.pro" is the right name. There could be an overridable default. [...] >>> char *ptr; >>> >>> f = fopen(infile,"r"); >>> >>> if (f == NULL) { >>> printf("Can' open %s\n", infile); > > Why can't you open it? > > printf("Unable to open '%s':%s\n", infile, strerror(errno)); The standard doesn't require fopen() to set errno on failure (though I think it does on most systems, and POSIX requires it). [...] >>Most C programmers would probably write >> >> while (*ptr == ' ' || *ptr == '\t') >> ++ptr; > > Or better: > > while (isspace(*ptr)) ptr++; Or better: > while (isspace((unsigned char)*ptr)) ptr++; If plain char is signed and *ptr happens to be negative (and not equal to EOF), isspace(*ptr) call has undefined behavior. -- 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"