🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #1192

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

#1192
From: James Kuyper
Date: Fri, 06 Nov 2026 00:52
39 lines
2817 bytes
On 2026-11-05 18:42, 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;
> }
No general statement can be made about that issue. It depends entirely
upon the expression. The syntax rules of C (and the slightly different
grammar rules of C++) specify what order the parts of the logical
expression get evaluated in. If that happens to be the order that you
want, no parentheses are needed. Otherwise, one or more parentheses
might solve the problem.

For the most part, the grammar was fairly well designed for the purpose
of reducing the number of parentheses needed. For example, all
arithmetic operators have higher precedence than the comparison
operators, which in turn have higher priority than the logical
operators. That is why the parentheses you added above are unnecessary.
For historical reasons, bitwise or (|) and bitwise and (&) are
exceptions. Unfortunately, correcting that mistake would break backwards
compatibility, which is a fairly high priority with both the C and C++
committees.

It never hurts to insert parentheses to specify precisely the order of
evaluation that you want - except insofar as other people reading your
code who know that a given set of parentheses are unnecessary might
guess that you don't. If you don't want them to do that, take the time
to learn the grammar rules.
Technical Details
Message-ID: <11cj8ch$24f93$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>

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: James Kuyper <jameskuyper@alumni.caltech.edu>
Newsgroups: comp.lang.c++,comp.lang.c
Subject: Re: does an expression in a logical statement need parenthesis for
 proper evaluation?
Date: Thu, 5 Nov 2026 19:52:31 -0500
Organization: A noiseless patient Spider
Lines: 39
Message-ID: <11cj8ch$24f93$1@dont-email.me>
References: <11cj4a0$23796$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 06 Nov 2026 00:52:34 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="2243875"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/h99h+99iFQat1Pfko2xLqKxtOsP28C7M="; posting-host="c496f625e107a4950c233df735bcbe22"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:oOXuWHNQf0jIkLUDKBRJb7plhe8= sha256:yVP+WctZNvTlCAXfSlHFJjPhtaKKovCXxbKNV4AjA7s=
	sha1:ShcWGIp0X/CXUWXyq3y3FfB4b5w= sha256:hNGtI2fR44aKB+aHYulCLMCpQvr2tiGldbKOUUR4LLI=
Content-Language: en-US
In-Reply-To: <11cj4a0$23796$1@dont-email.me>