🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #1199

Re: does an expression in a logical statement need parenthesis for proper evaluation?

#1199
From: David Brown
Date: Fri, 06 Nov 2026 07:49
58 lines
3482 bytes
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.  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: <11ck0pr$2amtc$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-mail
References: <11cj4a0$23796$1@dont-email.me> <11cj994$24m06$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: David Brown <david.brown@hesbynett.no>
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 08:49:15 +0100
Organization: A noiseless patient Spider
Lines: 58
Message-ID: <11ck0pr$2amtc$1@dont-email.me>
References: <11cj4a0$23796$1@dont-email.me> <11cj994$24m06$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 07:49:16 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="2448300"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX18RvjX98bFSeidAvbQW64Hcodi6arFavwM="; posting-host="ab1057a5cce4cf262a2cabef0012b0c0"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:E+E/ZZbOZBne9VPTRgS127YXz+8= sha256:lkisqyiZPvIXsy3cp0nlq/nJwVa/gWS92kZxbJJz5ig=
	sha1:W30UoHeg4J2Ew5tq8ALGljTOxG0= sha256:G6gPKsb178hTJPIbhqKTUb9hfuEYMn87+pMu7BFAz40=
In-Reply-To: <11cj994$24m06$1@dont-email.me>
Content-Language: en-GB