Path: csiph.com!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c Subject: Re: Which side of bitwise OR is evaluated first? Date: Wed, 14 Apr 2021 08:06:49 -0700 Organization: A noiseless patient Spider Lines: 43 Message-ID: <8635vsj46u.fsf@linuxsc.com> References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: reader02.eternal-september.org; posting-host="e14ed2c9513d4c03c630dd7e5aa045d1"; logging-data="9419"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18POndxp4WeB2mtMPWcE8qfb/mfbYQ9rcI=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:q4i4qRq8qHEswHVHp4FWVmbuXGQ= sha1:WgzR9A5xSgyf9q6K1j/MApKV5Ao= Xref: csiph.com comp.lang.c:160097 Oğuz writes: > I wrote this: > > fp = fopen(optarg, "r"); > if (!fp || (readfrom(fp), ferror(fp)|fclose(fp))) > perror(optarg) > > `readfrom' is a function of type `void' that calls `getline' until > it returns -1. > > The reason why I wrote it this way is that I wanted to handle all > errors and close the file pointer in a single block. But I'm not > sure if `ferror' is guaranteed to be called before `fclose' there, > and the standard says once `fclose' is called on `fp', what > `ferror(fp)' will do is unspecified or something like that. > > So, should I change this or is it good (except being a bit > unreadable)? Two comments. One, don't listen to the advice of "experts" who say something is or is not readable. Instead try writing the code several different ways and pick the one that best expresses what it is you want to say. Two, for what you're doing here I would be inclined to try something like this: FILE *fp = fopen( optarg, "r" ); int read_bad = fp ? readfrom( fp ), ferror( fp ) : 0; int close_bad = fp ? fclose( fp ) : 0; if( !fp || read_bad || close_bad ) perror( optarg ); Written this way I find it easy to see at a glance what the code is trying to do, and what the condition is to control whether an error message should be printed. If that approach works for you, great. The key though is to look for a way of writing that most closely expresses what it is you want to say.