Article View: comp.lang.c
Article #1394Re: does an expression in a logical statement need parenthesis for proper evaluation?
From: Janis Papanagnou
Date: Mon, 09 Nov 2026 20:42
Date: Mon, 09 Nov 2026 20:42
76 lines
5024 bytes
5024 bytes
On 2026-11-09 12:00, James Kuyper wrote:
> On 2026-11-09 02:32, Andrey Tarasevich wrote:
>> On Thu 11/5/2026 3:42 PM, Lynn McGuire wrote:
>>> 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;
>>> }
>>>
>> The whole `j1 * i1 < 1` is an expression in this case. It is parsed in
>> accordance with the grammar, which ultimately makes it equivalent to
>> `(j1 * i1) < 1`.
>>
>> Your question seems to suggest that the `j1 * i1` part somehow qualifies
>> as an expression, while the rest of it does not. That's not how it
>> works. The language does not give the `j1 * i1` part any special status.
> You are correct, parentheses are not needed. However, there's a related
> question that could have a different answer: should an arithmetic
> expression that is an operand of a comparison expression be
> parenthesized - not because it needs the parenthesis, but to clarify
> what the grammar already guarantees?
I observed that most grammars are consistently defined and have the
semantics what one expects. ("C" with &, |, ^ being with this detail
the obvious exception.) In some other well-known languages you even
need parentheses in expressions with mixed types. (Remember Pascal
with expressions like "(12 < 42) AND (33 >= 21)"; but here you will
just get a syntax error when omitting the parentheses, so it's safe.)
The problem with "C", beyond the mentioned bit-operations case, is
that there had been just one integral "type" and all operations were
applicable between integers, "characters", and "booleans", and that
they all "make sense" here. So type-safety of a language plays a role,
presuming the language (not "C") has a sensible differentiated set of
types. While for arithmetic the rules are (mostly) as you'd expect
them to be I'm usually using parenthesis to signify precedence within
the bool type in complex expressions containing mixtures of '&&' and
'||' - that's of course my personal thing.
> I tend not to use unnecessary parentheses, and I know the grammar well
> enough to correctly identify them - but many people don't know the
> grammar that well, which might include the next person to maintain my
> code. There are reasonable arguments that I should use parentheses more
> than I actually do, for the benefit of such people.
I'm "a bit" unsettled by the idea of a programmer who is unfamiliar
with the language in general or the grammar specifically! (I know
there's such folks writing programs.) I wouldn't write programs in
ways to satisfy such folks. I'd certainly write programs in ways to
keep the program maintainable focusing on the language specifics not
imagining that the piece of code might be handled by a Neanderthal.
(But that's also a topic of management; will they employ professional
programmers, do they install coding guidelines, will their processes
include QA means, etc. That's handled primarily on an organizational
level. For private programming it's even less an issue to consider
the worst case of some uninitiated working with your programs.)
I think the most benefit for parentheses is to visually structure
expressions that are very complex. While other means support that,
like in-line spacing and formatting an expression across more lines,
the parentheses are the prominent way to make the expression actually
*do* what is written so are unambiguous (as is splitting an expression
explicitly into sub-expressions).
Janis
Technical Details
Message-ID:
<11ctb70$3svb5$4@dont-email.me>Path:
mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mailReferences:
<11cj4a0$23796$1@dont-email.me> <11crsub$11ikc$1@dont-email.me> <11cs94q$15ptp$1@dont-email.me>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: Janis Papanagnou <janis_papanagnou+ng@hotmail.com> Newsgroups: comp.lang.c++,comp.lang.c Subject: Re: does an expression in a logical statement need parenthesis for proper evaluation? Date: Mon, 9 Nov 2026 21:42:08 +0100 Organization: A noiseless patient Spider Lines: 76 Message-ID: <11ctb70$3svb5$4@dont-email.me> References: <11cj4a0$23796$1@dont-email.me> <11crsub$11ikc$1@dont-email.me> <11cs94q$15ptp$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Mon, 09 Nov 2026 20:42:09 +0000 (UTC) Injection-Info: dont-email.me; logging-data="4095333"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+QxB5hUJ6z1YOUcyqZWQCg"; posting-host="bc9ed5282c348213c7ff360219c746d7" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:HBGz1Nl/EMV8cLVD4dgcDD0yfsM= sha256:tXFOJwABu5W+QKBVKObF/veqMDk3R8Y4YQ2Y3H5TPOI= sha1:owEtUOLckD1yLmuvZvFdpL8zEfg= sha256:gubKhkB2/EZAs32+L7TGZQW0nrMfF/wE7MKUfuJTLM0= Content-Language: en-US In-Reply-To: <11cs94q$15ptp$1@dont-email.me>