Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

"No problem is so formidable that you can't walk away from it." -- C. Schulz


devel / comp.lang.c / How About Disallowing Assignments In Expressions?

SubjectAuthor
* How About Disallowing Assignments In Expressions?Lawrence D'Oliveiro
+* Re: How About Disallowing Assignments In Expressions?Malcolm McLean
|`* Re: How About Disallowing Assignments In Expressions?Lawrence D'Oliveiro
| `- Re: How About Disallowing Assignments In Expressions?Tim Rentsch
+- Re: How About Disallowing Assignments In Expressions?Kaz Kylheku
+* Re: How About Disallowing Assignments In Expressions?David Brown
|+* Re: How About Disallowing Assignments In Expressions?Ben Bacarisse
||+* Re: How About Disallowing Assignments In Expressions?Richard Harnden
|||`* Re: How About Disallowing Assignments In Expressions?Ben Bacarisse
||| `* Re: How About Disallowing Assignments In Expressions?Richard Harnden
|||  `* Re: How About Disallowing Assignments In Expressions?Ben Bacarisse
|||   `* Re: How About Disallowing Assignments In Expressions?Malcolm McLean
|||    `* Re: How About Disallowing Assignments In Expressions?Ben Bacarisse
|||     `- Re: How About Disallowing Assignments In Expressions?Malcolm McLean
||`* Re: How About Disallowing Assignments In Expressions?David Brown
|| +* Re: How About Disallowing Assignments In Expressions?Ben Bacarisse
|| |`- Re: How About Disallowing Assignments In Expressions?David Brown
|| `- Re: How About Disallowing Assignments In Expressions?Janis Papanagnou
|+- Re: How About Disallowing Assignments In Expressions?Kaz Kylheku
|`* Re: How About Disallowing Assignments In Expressions?Keith Thompson
| +* Re: How About Disallowing Assignments In Expressions?Malcolm McLean
| |+- Re: How About Disallowing Assignments In Expressions?Keith Thompson
| |`* Re: How About Disallowing Assignments In Expressions?bart
| | +* Re: How About Disallowing Assignments In Expressions?fir
| | |`- Re: How About Disallowing Assignments In Expressions?fir
| | `* Re: How About Disallowing Assignments In Expressions?fir
| |  `* Re: How About Disallowing Assignments In Expressions?fir
| |   +- Re: How About Disallowing Assignments In Expressions?fir
| |   `- Re: How About Disallowing Assignments In Expressions?fir
| +* Re: How About Disallowing Assignments In Expressions?Kaz Kylheku
| |+- Re: How About Disallowing Assignments In Expressions?Keith Thompson
| |`* Re: How About Disallowing Assignments In Expressions?bart
| | +- Re: How About Disallowing Assignments In Expressions?Keith Thompson
| | `- Re: How About Disallowing Assignments In Expressions?Kaz Kylheku
| +- Re: How About Disallowing Assignments In Expressions?David Brown
| +* Re: How About Disallowing Assignments In Expressions?Keith Thompson
| |+- Re: How About Disallowing Assignments In Expressions?fir
| |`* Re: How About Disallowing Assignments In Expressions?Kaz Kylheku
| | `* Re: How About Disallowing Assignments In Expressions?dave thompson 2
| |  +* Re: How About Disallowing Assignments In Expressions?Janis Papanagnou
| |  |`- Re: How About Disallowing Assignments In Expressions?bart
| |  `- Re: How About Disallowing Assignments In Expressions?Lawrence D'Oliveiro
| +* Re: How About Disallowing Assignments In Expressions?Lawrence D'Oliveiro
| |+* Re: How About Disallowing Assignments In Expressions?Kaz Kylheku
| ||`* Re: How About Disallowing Assignments In Expressions?David Brown
| || `- Re: How About Disallowing Assignments In Expressions?Lawrence D'Oliveiro
| |+* Re: How About Disallowing Assignments In Expressions?Keith Thompson
| ||`* Re: How About Disallowing Assignments In Expressions?Keith Thompson
| || `* Re: How About Disallowing Assignments In Expressions?David Brown
| ||  +* Re: How About Disallowing Assignments In Expressions?Lawrence D'Oliveiro
| ||  |`- Re: How About Disallowing Assignments In Expressions?David Brown
| ||  `- Re: How About Disallowing Assignments In Expressions?Janis Papanagnou
| |`* Re: How About Disallowing Assignments In Expressions?Ben Bacarisse
| | +* Re: How About Disallowing Assignments In Expressions?David Brown
| | |+* Re: How About Disallowing Assignments In Expressions?Ben Bacarisse
| | ||+- Re: How About Disallowing Assignments In Expressions?Ben Bacarisse
| | ||+* Re: How About Disallowing Assignments In Expressions?bart
| | |||`- Re: How About Disallowing Assignments In Expressions?David Brown
| | ||+- Re: How About Disallowing Assignments In Expressions?Lawrence D'Oliveiro
| | ||`- Re: How About Disallowing Assignments In Expressions?David Brown
| | |`* Re: How About Disallowing Assignments In Expressions?Lawrence D'Oliveiro
| | | `* Re: How About Disallowing Assignments In Expressions?Ben Bacarisse
| | |  +* Re: How About Disallowing Assignments In Expressions?Lawrence D'Oliveiro
| | |  |`* Re: How About Disallowing Assignments In Expressions?Keith Thompson
| | |  | `* Re: How About Disallowing Assignments In Expressions?Lawrence D'Oliveiro
| | |  |  `* Re: How About Disallowing Assignments In Expressions?Keith Thompson
| | |  |   +* Re: How About Disallowing Assignments In Expressions?Lawrence D'Oliveiro
| | |  |   |`* Re: How About Disallowing Assignments In Expressions?Keith Thompson
| | |  |   | `* Re: How About Disallowing Assignments In Expressions?Lawrence D'Oliveiro
| | |  |   |  `* Re: How About Disallowing Assignments In Expressions?Keith Thompson
| | |  |   |   +* Re: How About Disallowing Assignments In Expressions?Keith Thompson
| | |  |   |   |`* Re: How About Disallowing Assignments In Expressions?David Brown
| | |  |   |   | `* Re: How About Disallowing Assignments In Expressions?bart
| | |  |   |   |  `* Re: How About Disallowing Assignments In Expressions?David Brown
| | |  |   |   |   +* Re: How About Disallowing Assignments In Expressions?bart
| | |  |   |   |   |`* Re: How About Disallowing Assignments In Expressions?David Brown
| | |  |   |   |   | `- Re: How About Disallowing Assignments In Expressions?Keith Thompson
| | |  |   |   |   `* Re: How About Disallowing Assignments In Expressions?Keith Thompson
| | |  |   |   |    `- Re: How About Disallowing Assignments In Expressions?David Brown
| | |  |   |   `* Re: How About Disallowing Assignments In Expressions?Lawrence D'Oliveiro
| | |  |   |    +- Re: How About Disallowing Assignments In Expressions?Keith Thompson
| | |  |   |    `* Re: How About Disallowing Assignments In Expressions?David Brown
| | |  |   |     `* Re: How About Disallowing Assignments In Expressions?Lawrence D'Oliveiro
| | |  |   |      +* Re: How About Disallowing Assignments In Expressions?Keith Thompson
| | |  |   |      |`* Re: How About Disallowing Assignments In Expressions?Lawrence D'Oliveiro
| | |  |   |      | +- Re: How About Disallowing Assignments In Expressions?Ben Bacarisse
| | |  |   |      | `- Re: How About Disallowing Assignments In Expressions?Keith Thompson
| | |  |   |      `- Re: How About Disallowing Assignments In Expressions?David Brown
| | |  |   `* Re: How About Disallowing Assignments In Expressions?Malcolm McLean
| | |  |    `* Re: How About Disallowing Assignments In Expressions?David Brown
| | |  |     +* Re: How About Disallowing Assignments In Expressions?Malcolm McLean
| | |  |     |`* Re: How About Disallowing Assignments In Expressions?David Brown
| | |  |     | `* Re: How About Disallowing Assignments In Expressions?Malcolm McLean
| | |  |     |  `* Re: How About Disallowing Assignments In Expressions?David Brown
| | |  |     |   `* Re: How About Disallowing Assignments In Expressions?Malcolm McLean
| | |  |     |    `* Re: How About Disallowing Assignments In Expressions?David Brown
| | |  |     |     +* Re: How About Disallowing Assignments In Expressions?Malcolm McLean
| | |  |     |     |`- Re: How About Disallowing Assignments In Expressions?David Brown
| | |  |     |     `* Re: How About Disallowing Assignments In Expressions?bart
| | |  |     |      +- Re: How About Disallowing Assignments In Expressions?Ben Bacarisse
| | |  |     |      `* Re: How About Disallowing Assignments In Expressions?David Brown
| | |  |     +* Re: How About Disallowing Assignments In Expressions?Lawrence D'Oliveiro
| | |  |     `* Re: How About Disallowing Assignments In Expressions?Janis Papanagnou
| | |  `* Re: How About Disallowing Assignments In Expressions?David Brown
| | +* Re: How About Disallowing Assignments In Expressions?Lawrence D'Oliveiro
| | `- Re: How About Disallowing Assignments In Expressions?Keith Thompson
| +- Re: How About Disallowing Assignments In Expressions?Janis Papanagnou
| `* Re: How About Disallowing Assignments In Expressions?Michael S
+* Re: How About Disallowing Assignments In Expressions?bart
+* Re: How About Disallowing Assignments In Expressions?Blue-Maned_Hawk
+- Re: How About Disallowing Assignments In Expressions?Richard Kettlewell
`* Re: How About Disallowing Assignments In Expressions?Thiago Adams

Pages:123456789101112131415161718192021
How About Disallowing Assignments In Expressions?

<uq3s76$28dsr$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=32906&group=comp.lang.c#32906

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.hispagatos.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ldo...@nz.invalid (Lawrence D'Oliveiro)
Newsgroups: comp.lang.c
Subject: How About Disallowing Assignments In Expressions?
Date: Fri, 9 Feb 2024 00:39:02 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 11
Message-ID: <uq3s76$28dsr$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 9 Feb 2024 00:39:02 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="6b81b1b5b0886ece37827f7a23070dda";
logging-data="2373531"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+aOsbfoBEZJvAD+zAlo1cY"
User-Agent: Pan/0.155 (Kherson; fc5a80b8)
Cancel-Lock: sha1:ISdVgkmOgJwD6TxCOK4AXln/ICc=
 by: Lawrence D'Oliv - Fri, 9 Feb 2024 00:39 UTC

If you want to make C a safer language, one obvious thing is to disallow
using “=” in the middle of an expression. Instead of returning the value
of the expression, have it return void. Or just disallow it syntactically
altogether.

Sure, you may still want to write something like

a = b = c;

but Python has found a way to allow that, without saying that “b = c” has
to return a value.

Re: How About Disallowing Assignments In Expressions?

<uq3t2m$28nj4$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=32908&group=comp.lang.c#32908

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: malcolm....@gmail.com (Malcolm McLean)
Newsgroups: comp.lang.c
Subject: Re: How About Disallowing Assignments In Expressions?
Date: Fri, 9 Feb 2024 00:53:42 +0000
Organization: A noiseless patient Spider
Lines: 21
Message-ID: <uq3t2m$28nj4$1@dont-email.me>
References: <uq3s76$28dsr$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 9 Feb 2024 00:53:42 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="cdcfcf1b9afae2cf895d8383e3c2747d";
logging-data="2383460"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/ozGarNq7bwlDc2Q24WRgYwC/X5vRSENs="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:yRPaqQ8ya7WirR/p8E40lOLFJDc=
In-Reply-To: <uq3s76$28dsr$1@dont-email.me>
Content-Language: en-GB
 by: Malcolm McLean - Fri, 9 Feb 2024 00:53 UTC

On 09/02/2024 00:39, Lawrence D'Oliveiro wrote:
> If you want to make C a safer language, one obvious thing is to disallow
> using “=” in the middle of an expression. Instead of returning the value
> of the expression, have it return void. Or just disallow it syntactically
> altogether.
>
> Sure, you may still want to write something like
>
> a = b = c;
>
> but Python has found a way to allow that, without saying that “b = c” has
> to return a value.

while( (ch = fgetc(fp)) != EOF)

is the idiomatic way of accessing a stream a character by character in
C. So you would disallow this.
--
Check out Basic Algorithms and my other books:
https://www.lulu.com/spotlight/bgy1mm

Re: How About Disallowing Assignments In Expressions?

<20240208170216.450@kylheku.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=32910&group=comp.lang.c#32910

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 433-929-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c
Subject: Re: How About Disallowing Assignments In Expressions?
Date: Fri, 9 Feb 2024 01:13:23 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 55
Message-ID: <20240208170216.450@kylheku.com>
References: <uq3s76$28dsr$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 9 Feb 2024 01:13:23 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="f0d9c4d56e1bf3d087a60d362bc7520c";
logging-data="2398553"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/JBmJ4y0vo4ZXoimUVw0MNLe9Tvo6LWAg="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:PUsYwgJfim78UB7vTtrOtBjJBhU=
 by: Kaz Kylheku - Fri, 9 Feb 2024 01:13 UTC

On 2024-02-09, Lawrence D'Oliveiro <ldo@nz.invalid> wrote:
> If you want to make C a safer language, one obvious thing is to disallow
> using “=” in the middle of an expression. Instead of returning the value
> of the expression, have it return void. Or just disallow it syntactically
> altogether.
>
> Sure, you may still want to write something like
>
> a = b = c;
>
> but Python has found a way to allow that, without saying that “b = c” has
> to return a value.

Python's original solution is stupid. All they had to do is to use :=
for assignment, and leave = as a comparison.

.... and that's what they did now, as of Python 3.8.

There is a := assignment that can be used in the middle of an
expression. This is given the idiotic nickname "walrus operator".

:= can be used in the middle of expressions.

Except they can't drop the old = assignment due to backward
compatibility.

Walrus is what should have been assignment in the first place.
But because Van Possum was coding Python in C, he just aped
some C choices into the language.

(I'm guessing that he knew that the first people to get their hands on
Python and popularize it would be C-and-Unix people, who would
balk at a := operator.)

By the way, assignment in the middle of expressions comes from Lisp.
Basically the idea that everything is an expression, even things
that perform side effects or behave like statements. The ternary
operator comes from Lisp: (if expr then else).

[CLISP]
[1]> (let ((i 0)) (list (incf i) (incf i) (incf i)))
(1 2 3)

You're unlikely to type setf or incf when you wanted a comparison.
ANSI Common Lisp is strictly evaluated so the above is required
to produce (1 2 3) and not (2 1 3) or whatever. Stuffing side
effects into Lisp expressions is less risky than in C.

(Not so with Scheme: in Scheme, the evaluation order of function
arguemnts is unspecified, like in C!)

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Re: How About Disallowing Assignments In Expressions?

<uq41in$2a4jh$3@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=32919&group=comp.lang.c#32919

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ldo...@nz.invalid (Lawrence D'Oliveiro)
Newsgroups: comp.lang.c
Subject: Re: How About Disallowing Assignments In Expressions?
Date: Fri, 9 Feb 2024 02:10:31 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 8
Message-ID: <uq41in$2a4jh$3@dont-email.me>
References: <uq3s76$28dsr$1@dont-email.me> <uq3t2m$28nj4$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 9 Feb 2024 02:10:31 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="6b81b1b5b0886ece37827f7a23070dda";
logging-data="2429553"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18+zMFiTb4TRtszC5K1lyTs"
User-Agent: Pan/0.155 (Kherson; fc5a80b8)
Cancel-Lock: sha1:8pLZ+PVO9YaznYpe9w+kgi4zOC8=
 by: Lawrence D'Oliv - Fri, 9 Feb 2024 02:10 UTC

On Fri, 9 Feb 2024 00:53:42 +0000, Malcolm McLean wrote:

> while( (ch = fgetc(fp)) != EOF)
>
> is the idiomatic way of accessing a stream a character by character in
> C. So you would disallow this.

Yes!

Re: How About Disallowing Assignments In Expressions?

<uq4nmb$2hive$6@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=32930&group=comp.lang.c#32930

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: How About Disallowing Assignments In Expressions?
Date: Fri, 9 Feb 2024 09:27:55 +0100
Organization: A noiseless patient Spider
Lines: 16
Message-ID: <uq4nmb$2hive$6@dont-email.me>
References: <uq3s76$28dsr$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 9 Feb 2024 08:27:55 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="9c8bd9f8f89cf9a03af351ae8d81a71b";
logging-data="2673646"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19KqWmKgOM7p5th6RyENkcDNdFx6lUUnMg="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:0/BiBb++7/vNULjxnrNqx6CPg5o=
Content-Language: en-GB
In-Reply-To: <uq3s76$28dsr$1@dont-email.me>
 by: David Brown - Fri, 9 Feb 2024 08:27 UTC

On 09/02/2024 01:39, Lawrence D'Oliveiro wrote:
> If you want to make C a safer language, one obvious thing is to disallow
> using “=” in the middle of an expression. Instead of returning the value
> of the expression, have it return void. Or just disallow it syntactically
> altogether.
>

I think that C would have been a safer language if assignment had been a
statement, not an expression (such as is the case in Pascal, for
example). But I don't think it is remotely feasible to change this in C
- you are fifty years too late.

The nearest you can come to changes like this in C would be optional
warnings or checks in compilers and static analysers.

Re: How About Disallowing Assignments In Expressions?

<878r3uq6we.fsf@bsb.me.uk>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=32933&group=comp.lang.c#32933

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: How About Disallowing Assignments In Expressions?
Date: Fri, 09 Feb 2024 10:16:49 +0000
Organization: A noiseless patient Spider
Lines: 24
Message-ID: <878r3uq6we.fsf@bsb.me.uk>
References: <uq3s76$28dsr$1@dont-email.me> <uq4nmb$2hive$6@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: dont-email.me; posting-host="c235c3c701c62e624897d4b41eb7a3c2";
logging-data="2709440"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+mE/pKWZTvbnsEWAGcr4C0wttRNIrzHj4="
User-Agent: Gnus/5.13 (Gnus v5.13)
Cancel-Lock: sha1:GZPdAaAZAp35U5cBN1Zhro8mJFc=
sha1:rzLMFwHmuqm/Y7RBgq37/NT2Qvg=
X-BSB-Auth: 1.23764df7f158177d362c.20240209101649GMT.878r3uq6we.fsf@bsb.me.uk
 by: Ben Bacarisse - Fri, 9 Feb 2024 10:16 UTC

David Brown <david.brown@hesbynett.no> writes:

> On 09/02/2024 01:39, Lawrence D'Oliveiro wrote:
>> If you want to make C a safer language, one obvious thing is to disallow
>> using “=” in the middle of an expression. Instead of returning the value
>> of the expression, have it return void. Or just disallow it syntactically
>> altogether.
>
> I think that C would have been a safer language if assignment had been a
> statement, not an expression

Surely someone has studied this, no? There must be some paper
describing bugs "in the wild"* and categorising them by what language
features would have made them either impossible or less likely. I don't
know of any (it was not even remotely my field) but there is such a
large pool of data out there I can't imagine that it's not been done.

* I mean to exclude bugs that get found early. For example, I've never
seen a = vs. == bug that survived even the most crude tests (and most,
like many other typo bugs, don't survive compilation without being
picked up).

--
Ben.

Re: How About Disallowing Assignments In Expressions?

<uq4v6u$2j4pb$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=32937&group=comp.lang.c#32937

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: richard....@gmail.invalid (Richard Harnden)
Newsgroups: comp.lang.c
Subject: Re: How About Disallowing Assignments In Expressions?
Date: Fri, 9 Feb 2024 10:36:10 +0000
Organization: A noiseless patient Spider
Lines: 100
Message-ID: <uq4v6u$2j4pb$1@dont-email.me>
References: <uq3s76$28dsr$1@dont-email.me> <uq4nmb$2hive$6@dont-email.me>
<878r3uq6we.fsf@bsb.me.uk>
Reply-To: richard.harnden@invalid.com
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 9 Feb 2024 10:36:14 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="949edf34bf7058090c8e8cb2977bbeb3";
logging-data="2724651"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+uKuTDA4742u0PwPkWlVDM0AksyLI6+PA="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:8HxAhlr7LMRay6Vm91L01T/cpnM=
In-Reply-To: <878r3uq6we.fsf@bsb.me.uk>
Content-Language: en-GB
 by: Richard Harnden - Fri, 9 Feb 2024 10:36 UTC

On 09/02/2024 10:16, Ben Bacarisse wrote:
> David Brown <david.brown@hesbynett.no> writes:
>
>> On 09/02/2024 01:39, Lawrence D'Oliveiro wrote:
>>> If you want to make C a safer language, one obvious thing is to disallow
>>> using “=” in the middle of an expression. Instead of returning the value
>>> of the expression, have it return void. Or just disallow it syntactically
>>> altogether.
>>
>> I think that C would have been a safer language if assignment had been a
>> statement, not an expression
>
> Surely someone has studied this, no? There must be some paper
> describing bugs "in the wild"* and categorising them by what language
> features would have made them either impossible or less likely. I don't
> know of any (it was not even remotely my field) but there is such a
> large pool of data out there I can't imagine that it's not been done.
>
> * I mean to exclude bugs that get found early. For example, I've never
> seen a = vs. == bug that survived even the most crude tests (and most,
> like many other typo bugs, don't survive compilation without being
> picked up).
>

This is what ChatGPT says ...

User
Can you list C language bugs, categorising them by what language
features would have made them either impossible or less likely.

ChatGPT
Certainly! Here are some common bugs in C categorized by language
features that could have prevented them or made them less likely:

Null Pointer Dereference:
Language Feature: Safe pointer handling like in Rust or smart pointers
in C++. Features like optional nullability annotations or language
constructs that enforce non-null pointers can help prevent null pointer
dereference bugs.

Buffer Overflows:
Language Feature: Bounds checking in arrays. Languages like Rust have
built-in checks to prevent buffer overflows at compile-time.
Safe string handling functions like those provided by libraries such as
Safe C String Library (SCL) or SafeLib.

Memory Leaks:
Language Feature: Automatic memory management like garbage collection or
smart pointers. Languages like Java, C#, and modern C++ with RAII
(Resource Acquisition Is Initialization) significantly reduce the
occurrence of memory leaks.

Use After Free:
Language Feature: Automated memory management, such as garbage
collection, or ownership tracking systems like those found in Rust's
ownership model.

Uninitialized Variables:
Language Feature: Initialization guarantees or compiler warnings for
uninitialized variables. Tools like static code analyzers or compilers
with strong type checking can help detect such issues.

Type Safety Violations:
Language Feature: Strong static typing. Languages like Java, C#, or Rust
enforce stronger type safety, reducing the likelihood of type-related bugs.

Race Conditions:
Language Feature: Built-in support for concurrent programming paradigms,
such as message passing (e.g., Erlang) or ownership model (e.g., Rust),
or immutability (e.g., functional programming languages like Haskell).

Integer Overflows:
Language Feature: Safe arithmetic operations. Languages like Rust
provide safe arithmetic operations that panic on overflow, preventing
undefined behavior.

Resource Management Issues (e.g., File Handles, Sockets):
Language Feature: RAII (Resource Acquisition Is Initialization) or using
constructs like "defer" in Go. Languages like Rust with its ownership
model can enforce proper resource management.

Unprotected Access to Shared Resources:
Language Feature: Mechanisms for synchronization and concurrent access
control, such as locks, semaphores, or higher-level constructs like
channels in Go or message passing in Erlang.

Floating Point Precision Errors:
Language Feature: Enhanced floating-point arithmetic support or
arbitrary-precision arithmetic libraries. Languages like Python or
libraries like GNU MPFR provide arbitrary-precision arithmetic, reducing
precision errors.

Pointer Arithmetic Errors:
Language Feature: Restricted or controlled pointer arithmetic. Safe
pointer operations and bounds checking can prevent pointer arithmetic
errors.

By incorporating these language features or using languages with
built-in protections against common programming errors, developers can
significantly reduce the likelihood of encountering these bugs.

Re: How About Disallowing Assignments In Expressions?

<87le7tpyig.fsf@bsb.me.uk>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=32945&group=comp.lang.c#32945

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!newsfeed.endofthelinebbs.com!nyheter.lysator.liu.se!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: How About Disallowing Assignments In Expressions?
Date: Fri, 09 Feb 2024 13:17:59 +0000
Organization: A noiseless patient Spider
Lines: 29
Message-ID: <87le7tpyig.fsf@bsb.me.uk>
References: <uq3s76$28dsr$1@dont-email.me> <uq4nmb$2hive$6@dont-email.me>
<878r3uq6we.fsf@bsb.me.uk> <uq4v6u$2j4pb$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: dont-email.me; posting-host="c235c3c701c62e624897d4b41eb7a3c2";
logging-data="2767696"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/rweRE6pE45LDIzwhIE8p6YQN7dHYv+5U="
User-Agent: Gnus/5.13 (Gnus v5.13)
Cancel-Lock: sha1:0pYKkh9syyjGQSaW25Ifs8XSTc4=
sha1:2rAyFw3WeiHzgZDdL2mgLdd6U6I=
X-BSB-Auth: 1.9f77301f709b6a425e45.20240209131759GMT.87le7tpyig.fsf@bsb.me.uk
 by: Ben Bacarisse - Fri, 9 Feb 2024 13:17 UTC

Richard Harnden <richard.nospam@gmail.invalid> writes:

> On 09/02/2024 10:16, Ben Bacarisse wrote:
>> David Brown <david.brown@hesbynett.no> writes:
>>
>>> On 09/02/2024 01:39, Lawrence D'Oliveiro wrote:
>>>> If you want to make C a safer language, one obvious thing is to disallow
>>>> using “=” in the middle of an expression. Instead of returning the value
>>>> of the expression, have it return void. Or just disallow it syntactically
>>>> altogether.
>>>
>>> I think that C would have been a safer language if assignment had been a
>>> statement, not an expression
>> Surely someone has studied this, no? There must be some paper
>> describing bugs "in the wild"* and categorising them by what language
>> features would have made them either impossible or less likely. I don't
>> know of any (it was not even remotely my field) but there is such a
>> large pool of data out there I can't imagine that it's not been done.
>> * I mean to exclude bugs that get found early. For example, I've never
>> seen a = vs. == bug that survived even the most crude tests (and most,
>> like many other typo bugs, don't survive compilation without being
>> picked up).
>
> This is what ChatGPT says ...

Seriously? You do know how large language models work, yes?

--
Ben.

Re: How About Disallowing Assignments In Expressions?

<uq5a6r$2lc87$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=32947&group=comp.lang.c#32947

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: How About Disallowing Assignments In Expressions?
Date: Fri, 9 Feb 2024 14:43:54 +0100
Organization: A noiseless patient Spider
Lines: 55
Message-ID: <uq5a6r$2lc87$1@dont-email.me>
References: <uq3s76$28dsr$1@dont-email.me> <uq4nmb$2hive$6@dont-email.me>
<878r3uq6we.fsf@bsb.me.uk>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 9 Feb 2024 13:43:55 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="9c8bd9f8f89cf9a03af351ae8d81a71b";
logging-data="2797831"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+QsHr6nAbMcFG+rVK7GZZHyQxLY0tWYb8="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:w+IyfM51YWmO4AN2t8SJCa3TNoU=
In-Reply-To: <878r3uq6we.fsf@bsb.me.uk>
Content-Language: en-GB
 by: David Brown - Fri, 9 Feb 2024 13:43 UTC

On 09/02/2024 11:16, Ben Bacarisse wrote:
> David Brown <david.brown@hesbynett.no> writes:
>
>> On 09/02/2024 01:39, Lawrence D'Oliveiro wrote:
>>> If you want to make C a safer language, one obvious thing is to disallow
>>> using “=” in the middle of an expression. Instead of returning the value
>>> of the expression, have it return void. Or just disallow it syntactically
>>> altogether.
>>
>> I think that C would have been a safer language if assignment had been a
>> statement, not an expression
>
> Surely someone has studied this, no? There must be some paper
> describing bugs "in the wild"* and categorising them by what language
> features would have made them either impossible or less likely. I don't
> know of any (it was not even remotely my field) but there is such a
> large pool of data out there I can't imagine that it's not been done.

I think it is very difficult to get good statistics on this kind of
thing. You can't really do a controlled study comparing one group
programming in "C where assignment is an expression" with a group
programming in "C where assignment is a statement". And you certainly
can't do a double-blind placebo controlled test!

There are plenty of coding standards which ban the use of the results of
assignment expressions, and I think there is little doubt that stricter
coding standards reduce at least some kinds of errors in code. But it
would be impossible to isolate the effect of one particular rule.

If someone knows about such studies, it would be great to hear about
them - failing that, guesses and gut feelings based on personal
experience is about as good as we can get.

>
> * I mean to exclude bugs that get found early. For example, I've never
> seen a = vs. == bug that survived even the most crude tests (and most,
> like many other typo bugs, don't survive compilation without being
> picked up).
>

I would agree with that.

However, I /have/ seen such bugs in code that has been written and
deployed, precisely because even the most crude tests and static
checking where not employed.

Far and away the biggest steps towards "safe C" is for people to use
good development practices, including good testing, good static error
checking, appropriate training, code reviews, etc. The unfortunate
reality is that there are many programmers who don't use such practices,
or don't use them correctly. And I don't think a change to how
assignment works would make a noticeable difference to this much larger
and more general problem.

Re: How About Disallowing Assignments In Expressions?

<uq5blg$2ljq3$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=32952&group=comp.lang.c#32952

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: richard....@gmail.invalid (Richard Harnden)
Newsgroups: comp.lang.c
Subject: Re: How About Disallowing Assignments In Expressions?
Date: Fri, 9 Feb 2024 14:08:48 +0000
Organization: A noiseless patient Spider
Lines: 35
Message-ID: <uq5blg$2ljq3$1@dont-email.me>
References: <uq3s76$28dsr$1@dont-email.me> <uq4nmb$2hive$6@dont-email.me>
<878r3uq6we.fsf@bsb.me.uk> <uq4v6u$2j4pb$1@dont-email.me>
<87le7tpyig.fsf@bsb.me.uk>
Reply-To: richard.harnden@invalid.com
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 9 Feb 2024 14:08:48 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="949edf34bf7058090c8e8cb2977bbeb3";
logging-data="2805571"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/ZG5gGPJUQN0Suh3YHKaG0CCyJ6W9JrUs="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:/nhkOj/EkRAaKQzffAP3uImVwSw=
In-Reply-To: <87le7tpyig.fsf@bsb.me.uk>
Content-Language: en-GB
 by: Richard Harnden - Fri, 9 Feb 2024 14:08 UTC

On 09/02/2024 13:17, Ben Bacarisse wrote:
> Richard Harnden <richard.nospam@gmail.invalid> writes:
>
>> On 09/02/2024 10:16, Ben Bacarisse wrote:
>>> David Brown <david.brown@hesbynett.no> writes:
>>>
>>>> On 09/02/2024 01:39, Lawrence D'Oliveiro wrote:
>>>>> If you want to make C a safer language, one obvious thing is to disallow
>>>>> using “=” in the middle of an expression. Instead of returning the value
>>>>> of the expression, have it return void. Or just disallow it syntactically
>>>>> altogether.
>>>>
>>>> I think that C would have been a safer language if assignment had been a
>>>> statement, not an expression
>>> Surely someone has studied this, no? There must be some paper
>>> describing bugs "in the wild"* and categorising them by what language
>>> features would have made them either impossible or less likely. I don't
>>> know of any (it was not even remotely my field) but there is such a
>>> large pool of data out there I can't imagine that it's not been done.
>>> * I mean to exclude bugs that get found early. For example, I've never
>>> seen a = vs. == bug that survived even the most crude tests (and most,
>>> like many other typo bugs, don't survive compilation without being
>>> picked up).
>>
>> This is what ChatGPT says ...
>
> Seriously? You do know how large language models work, yes?
>

Sure: It has read the entire internet - especially the the bit about
how Rust is better than C, apparently - and regurgitates the same.

Considering my five seconds of effort; I didn't think it was /that/ bad
of a list.

Re: How About Disallowing Assignments In Expressions?

<uq5h7r$2mgb3$2@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=32957&group=comp.lang.c#32957

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.chmurka.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (bart)
Newsgroups: comp.lang.c
Subject: Re: How About Disallowing Assignments In Expressions?
Date: Fri, 9 Feb 2024 15:43:55 +0000
Organization: A noiseless patient Spider
Lines: 29
Message-ID: <uq5h7r$2mgb3$2@dont-email.me>
References: <uq3s76$28dsr$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 9 Feb 2024 15:43:55 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="7249358d931a886a5d72096d92eeb1eb";
logging-data="2834787"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19hJHbNvd/XAcNPkgvDK+w1W5Le4QKs46w="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:HfGmBXP2HygR39uQNMBKg8XAdUI=
In-Reply-To: <uq3s76$28dsr$1@dont-email.me>
Content-Language: en-GB
 by: bart - Fri, 9 Feb 2024 15:43 UTC

On 09/02/2024 00:39, Lawrence D'Oliveiro wrote:
> If you want to make C a safer language, one obvious thing is to disallow
> using “=” in the middle of an expression. Instead of returning the value
> of the expression, have it return void. Or just disallow it syntactically
> altogether.
>
> Sure, you may still want to write something like
>
> a = b = c;
>
> but Python has found a way to allow that, without saying that “b = c” has
> to return a value.

I suspect that quite a number of existing programs will become invalid.

But what is the problem with 'a = b = c'? The real problems with
assignments inside expressions is when you accidentally use '=' instead
of '=='. That's due to a poor choice of syntax.

What I wouldn't care about banning however is this:

a += b += c;

Because it's just confusing. If you don't think so, consider a, b, c all
having different numerical types.

(It's also a bit of a pain when implementing a language that allows it.)

Re: How About Disallowing Assignments In Expressions?

<uq5i0r$2mkc4$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=32959&group=comp.lang.c#32959

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.bbs.nz!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: How About Disallowing Assignments In Expressions?
Date: Fri, 9 Feb 2024 16:57:15 +0100
Organization: A noiseless patient Spider
Lines: 59
Message-ID: <uq5i0r$2mkc4$1@dont-email.me>
References: <uq3s76$28dsr$1@dont-email.me> <uq5h7r$2mgb3$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 9 Feb 2024 15:57:15 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="9c8bd9f8f89cf9a03af351ae8d81a71b";
logging-data="2838916"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/T39qpCRY58Sllas25TPVXDeFp/EEHF6g="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:CDQ5j25BXinWg7UIpFBG6EBWRMA=
In-Reply-To: <uq5h7r$2mgb3$2@dont-email.me>
Content-Language: en-GB
 by: David Brown - Fri, 9 Feb 2024 15:57 UTC

On 09/02/2024 16:43, bart wrote:
> On 09/02/2024 00:39, Lawrence D'Oliveiro wrote:
>> If you want to make C a safer language, one obvious thing is to disallow
>> using “=” in the middle of an expression. Instead of returning the value
>> of the expression, have it return void. Or just disallow it syntactically
>> altogether.
>>
>> Sure, you may still want to write something like
>>
>>      a = b = c;
>>
>> but Python has found a way to allow that, without saying that “b = c” has
>> to return a value.
>
>
> I suspect that quite a number of existing programs will become invalid.

Yes. It's not used often IME, and would be easy to fix, but you only
need it once in a program to make such a suggestion a breaking change.

>
> But what is the problem with 'a = b = c'?

My preference is to do one thing at a time - then it's clear what is
going on. I think it is rare that you could make use of such chained
assignment, and even rarer where you could reasonably argue that it is
clearer than simply writing two separate lines. (And when you use a
"declare variables when you are ready to initialise them" style, it is
much more natural to use two lines.)

About the only time when I think you might say that chained assignment
is clearer is for very closely related variables set to zero :

int x, y, z;
x = y = z = 0;

I would probably be happy to throw that clear usage out in order to stop
people making unclear usage of chained assignment.

> The real problems with
> assignments inside expressions is when you accidentally use '=' instead
> of '=='. That's due to a poor choice of syntax.
>

That's a non-issue for anyone who can use a compiler with static error
checking, because it is almost always easy to spot automatically.

> What I wouldn't care about banning however is this:
>
>     a += b += c;
>
> Because it's just confusing. If you don't think so, consider a, b, c all
> having different numerical types.

Agreed 100%.

(I hope that didn't shock you too much!)

Re: How About Disallowing Assignments In Expressions?

<20240209083048.729@kylheku.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=32965&group=comp.lang.c#32965

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 433-929-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c
Subject: Re: How About Disallowing Assignments In Expressions?
Date: Fri, 9 Feb 2024 16:35:45 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 28
Message-ID: <20240209083048.729@kylheku.com>
References: <uq3s76$28dsr$1@dont-email.me> <uq4nmb$2hive$6@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 9 Feb 2024 16:35:45 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="f0d9c4d56e1bf3d087a60d362bc7520c";
logging-data="2851156"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18+syR9rAhhY+mwSQ2TfnuekKEowgVv+3Y="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:05oVWd+KJcTyVj7mvNxFlZfdD4E=
 by: Kaz Kylheku - Fri, 9 Feb 2024 16:35 UTC

On 2024-02-09, David Brown <david.brown@hesbynett.no> wrote:
> On 09/02/2024 01:39, Lawrence D'Oliveiro wrote:
>> If you want to make C a safer language, one obvious thing is to disallow
>> using “=” in the middle of an expression. Instead of returning the value
>> of the expression, have it return void. Or just disallow it syntactically
>> altogether.
>>
>
> I think that C would have been a safer language if assignment had been a
> statement, not an expression (such as is the case in Pascal, for
> example). But I don't think it is remotely feasible to change this in C
> - you are fifty years too late.

It was probably Lisp that introduced the idea of assignment being an
expression (due to everything being an expression). It's a safe
language, and not less safe on that account. This is because you are
vanishingly unlikely to accidentally write (setq a b) when you intended
(eq a b), and if that were to happen, another pair of reviewing eyes
would likely not gloss over it.

Python has now reintroduced a form of assignment you can use
in the middle of an expression; the token is := which is unlikely
to be used by mistake instead of an equality test or whatnot.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Re: How About Disallowing Assignments In Expressions?

<87r0hlef8q.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=32968&group=comp.lang.c#32968

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: How About Disallowing Assignments In Expressions?
Date: Fri, 09 Feb 2024 09:09:41 -0800
Organization: None to speak of
Lines: 40
Message-ID: <87r0hlef8q.fsf@nosuchdomain.example.com>
References: <uq3s76$28dsr$1@dont-email.me> <uq4nmb$2hive$6@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: dont-email.me; posting-host="639f38d912c33920e0e5a36e0f09675f";
logging-data="2868620"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19qNE5+iq4mBqoajhx9dxsx"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:TdZ50R1NV/c7+tzDeGhEdYOM9nE=
sha1:B6kc+5TrZNfEcgAK1+MExBxWmPc=
 by: Keith Thompson - Fri, 9 Feb 2024 17:09 UTC

David Brown <david.brown@hesbynett.no> writes:
> On 09/02/2024 01:39, Lawrence D'Oliveiro wrote:
>> If you want to make C a safer language, one obvious thing is to disallow
>> using “=” in the middle of an expression. Instead of returning the value
>> of the expression, have it return void. Or just disallow it syntactically
>> altogether.
>
> I think that C would have been a safer language if assignment had been
> a statement, not an expression (such as is the case in Pascal, for
> example). But I don't think it is remotely feasible to change this in
> C - you are fifty years too late.
>
> The nearest you can come to changes like this in C would be optional
> warnings or checks in compilers and static analysers.

I suggest that the bug of using "=" where "==" was intended occurs
almost exclusively among inexperienced C programmers, particularly those
who are inexperienced with programming in general.

It's a mistake that's made because the programmer assumes that "="
is a comparison operator, as it is in mathematics and in some other
programming languages. It's an easy and understandable mistake
to make, but any reasonably experienced C programmer will have
internalized the fact that "=" means assignment, not comparison.
To a newbie, `if (x = y)` *looks like* a comparison. To an
experienced C programmer it doesn't.

(It's possible I'm overgeneralizing. For example, a programmer who
commonly switches between C and other languages might still mix up "="
and "==" occasionally.)

If C had used ":=" for assignment and "=" for comparison (and "=="
probably not used for anything), the problem would never have come up,
because ":=" doesn't look like a comparison even to a newbie. But it's
far too late to fix that in any language called "C".

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Medtronic
void Void(void) { Void(); } /* The recursive call of the void */

Re: How About Disallowing Assignments In Expressions?

<uq5n06$2nkds$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=32970&group=comp.lang.c#32970

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: malcolm....@gmail.com (Malcolm McLean)
Newsgroups: comp.lang.c
Subject: Re: How About Disallowing Assignments In Expressions?
Date: Fri, 9 Feb 2024 17:22:14 +0000
Organization: A noiseless patient Spider
Lines: 14
Message-ID: <uq5n06$2nkds$1@dont-email.me>
References: <uq3s76$28dsr$1@dont-email.me> <uq4nmb$2hive$6@dont-email.me>
<87r0hlef8q.fsf@nosuchdomain.example.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 9 Feb 2024 17:22:14 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="1670c388a502905c76c2cac209d9c149";
logging-data="2871740"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/OptmTpBNErXQQx7dBqMRnsmpAl3SmAHU="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:q6RIbKKn9f8n4YTT9KyhMl7K/Lg=
In-Reply-To: <87r0hlef8q.fsf@nosuchdomain.example.com>
Content-Language: en-GB
 by: Malcolm McLean - Fri, 9 Feb 2024 17:22 UTC

On 09/02/2024 17:09, Keith Thompson wrote:
>
> (It's possible I'm overgeneralizing. For example, a programmer who
> commonly switches between C and other languages might still mix up "="
> and "==" occasionally.)
>

I had do to my PhD in Fortran, for my sins. It's := for assignment and =
for comparison, and when I switched back to C, I was always writing "="
for "==".
--
Check out Basic Algorithms and my other books:
https://www.lulu.com/spotlight/bgy1mm

Re: How About Disallowing Assignments In Expressions?

<87jzndee2l.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=32971&group=comp.lang.c#32971

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.samoylyk.net!newsfeed.xs3.de!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: How About Disallowing Assignments In Expressions?
Date: Fri, 09 Feb 2024 09:34:58 -0800
Organization: None to speak of
Lines: 20
Message-ID: <87jzndee2l.fsf@nosuchdomain.example.com>
References: <uq3s76$28dsr$1@dont-email.me> <uq4nmb$2hive$6@dont-email.me>
<87r0hlef8q.fsf@nosuchdomain.example.com>
<uq5n06$2nkds$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="639f38d912c33920e0e5a36e0f09675f";
logging-data="2871694"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19llwJ7K9KiOGvvl0vE56Yp"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:5ytF1N1b/C1dkdJDL6X/LMKAAVk=
sha1:abjCXAqmT1cZZmZKbElLfUemEIs=
 by: Keith Thompson - Fri, 9 Feb 2024 17:34 UTC

Malcolm McLean <malcolm.arthur.mclean@gmail.com> writes:
> On 09/02/2024 17:09, Keith Thompson wrote:
>> (It's possible I'm overgeneralizing. For example, a programmer who
>> commonly switches between C and other languages might still mix up "="
>> and "==" occasionally.)
>
> I had do to my PhD in Fortran, for my sins. It's := for assignment and
> = for comparison, and when I switched back to C, I was always writing
> "=" for "==".

Are you sure you're not thinking of Pascal?

Fortan doesn't use := as far as I can tell. It uses = for assignment
and .EQ. (or ==) for equality. (I think the == form is relatively new;
it's there in Fortran 90.)

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Medtronic
void Void(void) { Void(); } /* The recursive call of the void */

Re: How About Disallowing Assignments In Expressions?

<20240209093537.386@kylheku.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=32972&group=comp.lang.c#32972

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!newsfeed.endofthelinebbs.com!nyheter.lysator.liu.se!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 433-929-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c
Subject: Re: How About Disallowing Assignments In Expressions?
Date: Fri, 9 Feb 2024 17:38:07 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 17
Message-ID: <20240209093537.386@kylheku.com>
References: <uq3s76$28dsr$1@dont-email.me> <uq4nmb$2hive$6@dont-email.me>
<87r0hlef8q.fsf@nosuchdomain.example.com>
Injection-Date: Fri, 9 Feb 2024 17:38:07 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="f0d9c4d56e1bf3d087a60d362bc7520c";
logging-data="2877021"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/LgTvyGydLcNh7rjIOcQ+fPkieXRytKEI="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:bNHV9yirKJUz+fJ+d06E6sZwJeo=
 by: Kaz Kylheku - Fri, 9 Feb 2024 17:38 UTC

On 2024-02-09, Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
> I suggest that the bug of using "=" where "==" was intended occurs
> almost exclusively among inexperienced C programmers, particularly those
> who are inexperienced with programming in general.

Almost exclusively. I've had this sneak upon me maybe once or twice in
the past 15 years. GCC warns well in most situations in which the typo
is made, so it doesn't easily sneak through.

This is in the bug category of "used to be menacing before we had better
diagnostics", along with forgetting to include <stdlib.h> before using
malloc.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Re: How About Disallowing Assignments In Expressions?

<87fry1edeo.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=32973&group=comp.lang.c#32973

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.samoylyk.net!newsfeed.xs3.de!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: How About Disallowing Assignments In Expressions?
Date: Fri, 09 Feb 2024 09:49:19 -0800
Organization: None to speak of
Lines: 27
Message-ID: <87fry1edeo.fsf@nosuchdomain.example.com>
References: <uq3s76$28dsr$1@dont-email.me> <uq4nmb$2hive$6@dont-email.me>
<87r0hlef8q.fsf@nosuchdomain.example.com>
<20240209093537.386@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="639f38d912c33920e0e5a36e0f09675f";
logging-data="2871694"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18nG6eYHjoLkGVa2HJSXAN3"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:RwNHHkGANItcaw+7ekzL5J3y6ZA=
sha1:5wULZxxxIJoCKXAO1Eko2PKvG5U=
 by: Keith Thompson - Fri, 9 Feb 2024 17:49 UTC

Kaz Kylheku <433-929-6894@kylheku.com> writes:
> On 2024-02-09, Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
>> I suggest that the bug of using "=" where "==" was intended occurs
>> almost exclusively among inexperienced C programmers, particularly those
>> who are inexperienced with programming in general.
>
> Almost exclusively. I've had this sneak upon me maybe once or twice in
> the past 15 years. GCC warns well in most situations in which the typo
> is made, so it doesn't easily sneak through.
>
> This is in the bug category of "used to be menacing before we had better
> diagnostics", along with forgetting to include <stdlib.h> before using
> malloc.

One issue with C is that its syntax is what I think of as "dense".

In more verbose languages (Ada is an example), a typo is very likely to
result in an easily diagnosable syntax error. In C, many typos result
in syntactically valid code that means something different. Examples
are = vs ==, > vs >>, etc. Compilers can detect and warn about some
things that are likely to be typos, like `if (a = b)`, but certain kinds
of errors are still easier to make in C than in some other languages.

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Medtronic
void Void(void) { Void(); } /* The recursive call of the void */

Re: How About Disallowing Assignments In Expressions?

<uq5pbq$2nvst$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=32974&group=comp.lang.c#32974

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (bart)
Newsgroups: comp.lang.c
Subject: Re: How About Disallowing Assignments In Expressions?
Date: Fri, 9 Feb 2024 18:02:34 +0000
Organization: A noiseless patient Spider
Lines: 24
Message-ID: <uq5pbq$2nvst$1@dont-email.me>
References: <uq3s76$28dsr$1@dont-email.me> <uq4nmb$2hive$6@dont-email.me>
<87r0hlef8q.fsf@nosuchdomain.example.com> <uq5n06$2nkds$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 9 Feb 2024 18:02:34 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="7249358d931a886a5d72096d92eeb1eb";
logging-data="2883485"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+Wa9YDhLiANwwlc8mugBDfkUbXPe3YlMo="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:h6BnauQNNZKCcTY8jOksw5dCGwk=
In-Reply-To: <uq5n06$2nkds$1@dont-email.me>
Content-Language: en-GB
 by: bart - Fri, 9 Feb 2024 18:02 UTC

On 09/02/2024 17:22, Malcolm McLean wrote:
> On 09/02/2024 17:09, Keith Thompson wrote:
>>
>> (It's possible I'm overgeneralizing.  For example, a programmer who
>> commonly switches between C and other languages might still mix up "="
>> and "==" occasionally.)
>>
>
> I had do to my PhD in Fortran, for my sins. It's := for assignment and =
> for comparison, and when I switched back to C, I was always writing "="
> for "==".

Fortran must have changed considerably since I last used it.

It was "=" to assign, and ".EQ." for equality.

I assume you mean another language. My normal one does use := and = like
that, and switching to/from C gives problems: you write = in C instead
of ==, and in mine I'd write = instead of :=. However:

a = b

was a no-op in my language (or rather, it compared then discarded the
result), leading to puzzling bugs. Now I usually detect such statements.

Re: How About Disallowing Assignments In Expressions?

<uq5pfp$2nvst$2@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=32975&group=comp.lang.c#32975

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (bart)
Newsgroups: comp.lang.c
Subject: Re: How About Disallowing Assignments In Expressions?
Date: Fri, 9 Feb 2024 18:04:41 +0000
Organization: A noiseless patient Spider
Lines: 20
Message-ID: <uq5pfp$2nvst$2@dont-email.me>
References: <uq3s76$28dsr$1@dont-email.me> <uq4nmb$2hive$6@dont-email.me>
<87r0hlef8q.fsf@nosuchdomain.example.com> <20240209093537.386@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 9 Feb 2024 18:04:41 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="7249358d931a886a5d72096d92eeb1eb";
logging-data="2883485"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19m7Hrt/ZOa1qI0K2q6eZHQo5IKip3+YYw="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:ouD5z2bJeHZxIxWZez03uFzBBTg=
In-Reply-To: <20240209093537.386@kylheku.com>
Content-Language: en-GB
 by: bart - Fri, 9 Feb 2024 18:04 UTC

On 09/02/2024 17:38, Kaz Kylheku wrote:
> On 2024-02-09, Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
>> I suggest that the bug of using "=" where "==" was intended occurs
>> almost exclusively among inexperienced C programmers, particularly those
>> who are inexperienced with programming in general.
>
> Almost exclusively. I've had this sneak upon me maybe once or twice in
> the past 15 years. GCC warns well in most situations in which the typo
> is made, so it doesn't easily sneak through.
>
> This is in the bug category of "used to be menacing before we had better
> diagnostics", along with forgetting to include <stdlib.h> before using
> malloc.
>

How does gcc know whether 'if (a = b)' is meant to be an assignment or
comparison?

Simply warning on the possibility is not going to be helpful, because
you will be swamped with output that may hide more important matters.

Re: How About Disallowing Assignments In Expressions?

<87bk8peblx.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=32978&group=comp.lang.c#32978

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: How About Disallowing Assignments In Expressions?
Date: Fri, 09 Feb 2024 10:28:10 -0800
Organization: None to speak of
Lines: 31
Message-ID: <87bk8peblx.fsf@nosuchdomain.example.com>
References: <uq3s76$28dsr$1@dont-email.me> <uq4nmb$2hive$6@dont-email.me>
<87r0hlef8q.fsf@nosuchdomain.example.com>
<20240209093537.386@kylheku.com> <uq5pfp$2nvst$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="639f38d912c33920e0e5a36e0f09675f";
logging-data="2893543"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/HQF+BO727Li0FB03IOS93"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:GOR4XdlOi0JpZh+1W6aqzTIyiJg=
sha1:xPg6a9C9nBjjLKIEkwSL9RcTADE=
 by: Keith Thompson - Fri, 9 Feb 2024 18:28 UTC

bart <bc@freeuk.com> writes:
> On 09/02/2024 17:38, Kaz Kylheku wrote:
>> On 2024-02-09, Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
>>> I suggest that the bug of using "=" where "==" was intended occurs
>>> almost exclusively among inexperienced C programmers, particularly those
>>> who are inexperienced with programming in general.
>> Almost exclusively. I've had this sneak upon me maybe once or twice
>> in the past 15 years. GCC warns well in most situations in which the
>> typo is made, so it doesn't easily sneak through. This is in the bug
>> category of "used to be menacing before we had better diagnostics",
>> along with forgetting to include <stdlib.h> before using malloc.
>
> How does gcc know whether 'if (a = b)' is meant to be an assignment or
> comparison?
>

It doesn't know. If you enable the warning (-Wparentheses, enabled by
-Wall), it assumes that an assignment at the top level of a condition
was probably meant to be a comparison, and warns about it. The warning
suggests adding parentheses, so `if ((a = b))` avoids the warning.

> Simply warning on the possibility is not going to be helpful, because
> you will be swamped with output that may hide more important matters.

Assignments in conditions are rare enough that you're not likely to be
swamped.

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Medtronic
void Void(void) { Void(); } /* The recursive call of the void */

Re: How About Disallowing Assignments In Expressions?

<uq5rtn$24oum$1@i2pn2.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=32981&group=comp.lang.c#32981

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!.POSTED!not-for-mail
From: fir...@grunge.pl (fir)
Newsgroups: comp.lang.c
Subject: Re: How About Disallowing Assignments In Expressions?
Date: Fri, 09 Feb 2024 19:46:09 +0100
Organization: i2pn2 (i2pn.org)
Message-ID: <uq5rtn$24oum$1@i2pn2.org>
References: <uq3s76$28dsr$1@dont-email.me> <uq4nmb$2hive$6@dont-email.me> <87r0hlef8q.fsf@nosuchdomain.example.com> <uq5n06$2nkds$1@dont-email.me> <uq5pbq$2nvst$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 9 Feb 2024 18:46:15 -0000 (UTC)
Injection-Info: i2pn2.org;
logging-data="2253782"; mail-complaints-to="usenet@i2pn2.org";
posting-account="+ydHcGjgSeBt3Wz3WTfKefUptpAWaXduqfw5xdfsuS0";
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:27.0) Gecko/20100101 Firefox/27.0 SeaMonkey/2.24
X-Spam-Checker-Version: SpamAssassin 4.0.0
In-Reply-To: <uq5pbq$2nvst$1@dont-email.me>
 by: fir - Fri, 9 Feb 2024 18:46 UTC

bart wrote:
> On 09/02/2024 17:22, Malcolm McLean wrote:
>> On 09/02/2024 17:09, Keith Thompson wrote:
>>>
>>> (It's possible I'm overgeneralizing. For example, a programmer who
>>> commonly switches between C and other languages might still mix up "="
>>> and "==" occasionally.)
>>>
>>
>> I had do to my PhD in Fortran, for my sins. It's := for assignment and
>> = for comparison, and when I switched back to C, I was always writing
>> "=" for "==".
>
> Fortran must have changed considerably since I last used it.
>
> It was "=" to assign, and ".EQ." for equality.
>
> I assume you mean another language. My normal one does use := and = like
> that, and switching to/from C gives problems: you write = in C instead
> of ==, and in mine I'd write = instead of :=. However:
>
> a = b
>
> was a no-op in my language (or rather, it compared then discarded the
> result), leading to puzzling bugs. Now I usually detect such statements.

as some maybe know im from time to time write my compiler on c based
langage and also i hardly think how to simplify and expand syntax and
construction

as to this problem how ewentually replace = and == at present time i
just found this problem unresolvable :c

what i think = coud eventually stay though it also could have
alternative.. == is in turn clearly wrong but one would need unicode
sign for this i guess.. there is need for some sign like = with a small
"?" above it

≟ possibly

a≟b

Re: How About Disallowing Assignments In Expressions?

<20240209104304.365@kylheku.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=32982&group=comp.lang.c#32982

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!newsfeed.endofthelinebbs.com!nyheter.lysator.liu.se!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 433-929-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c
Subject: Re: How About Disallowing Assignments In Expressions?
Date: Fri, 9 Feb 2024 18:52:35 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 55
Message-ID: <20240209104304.365@kylheku.com>
References: <uq3s76$28dsr$1@dont-email.me> <uq4nmb$2hive$6@dont-email.me>
<87r0hlef8q.fsf@nosuchdomain.example.com> <20240209093537.386@kylheku.com>
<uq5pfp$2nvst$2@dont-email.me>
Injection-Date: Fri, 9 Feb 2024 18:52:35 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="f0d9c4d56e1bf3d087a60d362bc7520c";
logging-data="2896892"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+Z/n/D12Q9zbsLM0fCTFCduIP4Rj3+9mE="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:GKkCTpziruTemU19ojbL3WnFgvs=
 by: Kaz Kylheku - Fri, 9 Feb 2024 18:52 UTC

On 2024-02-09, bart <bc@freeuk.com> wrote:
> On 09/02/2024 17:38, Kaz Kylheku wrote:
>> On 2024-02-09, Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
>>> I suggest that the bug of using "=" where "==" was intended occurs
>>> almost exclusively among inexperienced C programmers, particularly those
>>> who are inexperienced with programming in general.
>>
>> Almost exclusively. I've had this sneak upon me maybe once or twice in
>> the past 15 years. GCC warns well in most situations in which the typo
>> is made, so it doesn't easily sneak through.
>>
>> This is in the bug category of "used to be menacing before we had better
>> diagnostics", along with forgetting to include <stdlib.h> before using
>> malloc.
>>
>
> How does gcc know whether 'if (a = b)' is meant to be an assignment or
> comparison?

The point is that this is not knowable; it is ambiguous. (Perhaps
the current maintainer of the code doesn't even know).

That's why it deserves a diagnostic.

Someone will have to look at it and figure out whether it should
be the assignment or comparison.

> Simply warning on the possibility is not going to be helpful, because
> you will be swamped with output that may hide more important matters.

So you might suspect, but that turns out to be wrong.

GCC assumes that this is a mistake and warns about it, suggesting
that parentheses be put around the assignment if it is intended.

warning: suggest parentheses around assignment used as truth value
[-Wparentheses]

This -Wparentheses is included in -Wall. Thus:

if (a = b) // warns

if ((a = b)) // shuts up

this heuristic turns out to be good. The diagnostic rarely occurs in the
first place, and the fix is trivial.

Parentheses normally disambiguate associativity/precedence. So by a kind
of metaphor, here they are used to fix a psychological rather than
grammatical ambiguity.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Re: How About Disallowing Assignments In Expressions?

<uq5tua$2ooie$2@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=32984&group=comp.lang.c#32984

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!feeder8.news.weretis.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: How About Disallowing Assignments In Expressions?
Date: Fri, 9 Feb 2024 20:20:42 +0100
Organization: A noiseless patient Spider
Lines: 46
Message-ID: <uq5tua$2ooie$2@dont-email.me>
References: <uq3s76$28dsr$1@dont-email.me> <uq4nmb$2hive$6@dont-email.me>
<87r0hlef8q.fsf@nosuchdomain.example.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 9 Feb 2024 19:20:42 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="2ac7bc3d44bf8717c1088c337fb68b71";
logging-data="2908750"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18fezVvSof79NFUQo0TD0R73lTiNhF/bk4="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:+aQ9jM+t+U0JyL5R9oLKroS6hos=
Content-Language: en-GB
In-Reply-To: <87r0hlef8q.fsf@nosuchdomain.example.com>
 by: David Brown - Fri, 9 Feb 2024 19:20 UTC

On 09/02/2024 18:09, Keith Thompson wrote:
> David Brown <david.brown@hesbynett.no> writes:
>> On 09/02/2024 01:39, Lawrence D'Oliveiro wrote:
>>> If you want to make C a safer language, one obvious thing is to disallow
>>> using “=” in the middle of an expression. Instead of returning the value
>>> of the expression, have it return void. Or just disallow it syntactically
>>> altogether.
>>
>> I think that C would have been a safer language if assignment had been
>> a statement, not an expression (such as is the case in Pascal, for
>> example). But I don't think it is remotely feasible to change this in
>> C - you are fifty years too late.
>>
>> The nearest you can come to changes like this in C would be optional
>> warnings or checks in compilers and static analysers.
>
> I suggest that the bug of using "=" where "==" was intended occurs
> almost exclusively among inexperienced C programmers, particularly those
> who are inexperienced with programming in general.

It can also happen accidentally - even experienced programmers can make
typos which they don't spot immediately. But experienced programmers
will normally be using tools that will find such mistakes very quickly.

>
> It's a mistake that's made because the programmer assumes that "="
> is a comparison operator, as it is in mathematics and in some other
> programming languages. It's an easy and understandable mistake
> to make, but any reasonably experienced C programmer will have
> internalized the fact that "=" means assignment, not comparison.
> To a newbie, `if (x = y)` *looks like* a comparison. To an
> experienced C programmer it doesn't.
>
> (It's possible I'm overgeneralizing. For example, a programmer who
> commonly switches between C and other languages might still mix up "="
> and "==" occasionally.)
>
> If C had used ":=" for assignment and "=" for comparison (and "=="
> probably not used for anything), the problem would never have come up,
> because ":=" doesn't look like a comparison even to a newbie. But it's
> far too late to fix that in any language called "C".
>

Yes.

Re: How About Disallowing Assignments In Expressions?

<pan$68183$c20f769a$773bf670$77626466@invalid.invalid>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=32985&group=comp.lang.c#32985

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!bluemanedhawk.eternal-september.org!.POSTED!not-for-mail
From: bluemane...@invalid.invalid (Blue-Maned_Hawk)
Newsgroups: comp.lang.c
Subject: Re: How About Disallowing Assignments In Expressions?
Date: Fri, 9 Feb 2024 19:47:45 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 13
Message-ID: <pan$68183$c20f769a$773bf670$77626466@invalid.invalid>
References: <uq3s76$28dsr$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 9 Feb 2024 19:47:45 -0000 (UTC)
Injection-Info: bluemanedhawk.eternal-september.org; posting-host="4360eab8ab1f5b52990c25ac8247f24d";
logging-data="2919996"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX184sgX+ImKTo0/65Z0phzmK9nsXwyKhleM="
User-Agent: Pan/0.154 (Izium; 517acf4)
Cancel-Lock: sha1:y9ojvgnJwRoCVT1XYvThsRLwxuw=
X-Face: Llanfair­pwllgwyng
yll
­gogeryÂÃ
? ?­chwyrnÂÂÂÂÂÂ
­drobwll­llanÃ
‚Ã
ƒƒƒ‚­tysilioÂÂÂÃ
‚­gogo­goch
Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAIAAADYYG7QAAACh0lEQVRYw71Z21bD
MAzzevbfkr4cHjrSXJyL044+MDa6WLEl2SkvkrZ1AbAvXO+bUGSCPYnsuIVGMpm
ZLnjX718GhAKNsp8lON2F9VrhELwIgJlBepkZjA78rVK+FkmNhEJK76UsJlz8+E
rJsjrpYouhLo/SC6qPHgakFOR8wV9+8rCfO/I/oVnmUZUp42/LW2XkLj9TCFNM9
jp5g2EmHZgpYZjCOkYU7sXVogRylJqpdggoFLG1g09Flah/7kErCxzR9HgXPYsq
0glb9cxjIz2Vsk9AmAoCSxECpD713joMKjQqLAtmMqJmXjdVvlMnMQCVITotJd1
z+fh1f1NNo+vuc1KnhWUmY7t03vydTud9BbXCtN3L2PL3bK7JCNG0GHzuZxafyB
fxevCxpm1vrwZltqw6SILCcdoCE6PGQC8wZWDA9Or7Qp5s3lAZezys0nDazs9S9
R0TjwEiksRxLkNPC1NMMWPs1bj0Ei0Yuo+JVtFLuzP1NRJ16qXWN8DhhtmS4PDg
O6mqRxs4bEJrYt087mSIow/1VzW2oFlMQuiuIy/KsUagvhdw6hSjJGlIavbLF8x
j3X47bccLcUSi0dkWh1nUZNhANT1tHKUXrNxNLbd9KPb9wDDVrKwmPQMOPQ1oy6
k5I1DwzDeRJd3jVIhDAUxq3ngzJG4CCkNXZxZVMcjefoK2J0gUY2S3rxz/RuTFx
2zHd9U+obimJXMG4edsk/2j5pTU5G1MmzbRLxkfq5EiT1GGsidvMGzi+1goGb2l
GCrN+nGnV8xj3q3JLRDVPL96vUc7Z4aJ3TN1mVqWAMJMfG+Jxh6TQqP+92iZkCU
xtglds1AB6r0aiSHKcnFck+p/c/0CbacFLQcajGcAAAAASUVORK5CYII=
 by: Blue-Maned_Hawk - Fri, 9 Feb 2024 19:47 UTC

There are too many situations where assignment being an expression is a
useful facet of the language; i am not willing to sacrifice that for an
ill-defined ideal of “safety”.

--
Blue-Maned_Hawk│shortens to
Hawk│/
blu.mɛin.dÊ°ak/
│he/him/his/himself/Mr.
blue-maned_hawk.srht.site
It cost 53 million pounds just to cancel.

Pages:123456789101112131415161718192021
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor