Article View: comp.lang.c
Article #1218Re: does an expression in a logical statement need parenthesis for proper evaluation?
From: Chris M. Thomasson
Date: Fri, 06 Nov 2026 21:25
Date: Fri, 06 Nov 2026 21:25
67 lines
3829 bytes
3829 bytes
On 11/5/2026 11:49 PM, David Brown wrote:
> On 06/11/2026 02:07, Lynn McGuire wrote:
>> On 11/5/2026 5:42 PM, Lynn McGuire wrote:
>>> Hi,
>>>
>>> Does an expression in a logical statement need parenthesis for proper
>>> evaluation in C and/or C++?
>>>
>>> In other words, should
>>>
>>> if (j1 * i1 < 1) {
>>> goto L463;
>>> }
>>>
>>> be
>>>
>>> if ((j1 * i1) < 1) {
>>> goto L463;
>>> }
>>>
>>> Thanks,
>>> Lynn
>>
>> Thank you all very much. BTW, there were parenthesis in the old
>> Fortran code but F2C stripped them out. I put them back in for my
>> comfort as I see them.
>>
>
> Since you are generating the C++ code automatically, there should not be
> a problem adding the parentheses. Generated code that contains "goto
> L463" is clearly not intended for humans anyway (at least, not without a
> good deal of effort).
>
> There is a proportion of programmers who think it is somehow blasphemy
> to use more parentheses in an expression than strictly necessary. That
> type of programmer will also disapprove of the extra braces around the
> "goto" in your example.
>
> There is a proportion of programmers who think you should /always/ use
> parentheses to show the precedence of operators.
>
> Most programmers, I think, are in the middle. Parentheses here can make
> the expression clearer at a glance, but it is also obvious what it means
> when not using extra parentheses.
Agreed. I don't mind extra parentheses at all. Totally fine, and can
"help" in certain areas. I just happen to know about the order of
operations.
It is common to have parentheses in
> situations where the operator precedence is not as well known, or may
> differ between programming languages - mixing bitwise and logical
> operators and equality operators are examples.
>
> You can get a reasonable balance using gcc's warnings - these also have
> the big advantage of making it easy to check! "-Wparentheses" (enabled
> by -Wall) will catch, for example, "a < b == c" which parses as "a < (b
> == c)". If "-Wparentheses" does not lead to complaints, then your
> generated code is probably okay without the extra parentheses - but you
> can still add them here if you prefer.
>
> In general, I think you want your generated code to pass "gcc -O2 -Wall"
> cleanly (or the clang equivalent, which is /not/ -Weverything!). You
> may even aim for -Wextra.
>
Technical Details
Message-ID:
<11clgjo$2tgts$1@dont-email.me>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mailReferences:
<11cj4a0$23796$1@dont-email.me> <11cj994$24m06$1@dont-email.me> <11ck0pr$2amtc$1@dont-email.me>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> Newsgroups: comp.lang.c++,comp.lang.c Subject: Re: does an expression in a logical statement need parenthesis for proper evaluation? Date: Fri, 6 Nov 2026 13:25:10 -0800 Organization: A noiseless patient Spider Lines: 67 Message-ID: <11clgjo$2tgts$1@dont-email.me> References: <11cj4a0$23796$1@dont-email.me> <11cj994$24m06$1@dont-email.me> <11ck0pr$2amtc$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Fri, 06 Nov 2026 21:25:13 +0000 (UTC) Injection-Info: dont-email.me; logging-data="3064764"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+ysJJbhZW4A0PoNFfyhgnraOiSLGXD1O8="; posting-host="05131b9d92d665448b45480425c57f0b" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:f8WaACszVcbiF6Nkrh16yMVD4qg= sha256:vPUtz87TLUbMkptrLFYCeJsrfk/BwJQlwlbf4HwlIIU= sha1:fcw9vJ8uPOzZ+p00F6k1g6IbkLA= sha256:WhIgWsdEj0fXmoZOuxvagG4zIcsVqy4DNqxZdQVuZAw= Content-Language: en-US In-Reply-To: <11ck0pr$2amtc$1@dont-email.me>