Re: Origins of == in C

David Toland <det@sw.stratus.com>
Thu, 3 Aug 1995 15:02:31 GMT

          From comp.compilers

Related articles
Origins of == in C jpowers@ti.com (1995-07-20)
Re: Origins of == in C D.Chappell@biochem.usyd.edu.au (Doug CHAPPELL) (1995-07-25)
Re: Origins of == in C Brendan.Gowing@cs.tcd.ie (Brendan Gowing) (1995-07-26)
Re: Origins of == in C peter.schurek@banyan.siemens.co.at (1995-07-28)
Re: Origins of == in C schrod@iti.informatik.th-darmstadt.de (1995-07-31)
Re: Origins of == in C jmccarty@spdmail.spd.dsccc.com (1995-08-01)
Re: Origins of == in C det@sw.stratus.com (David Toland) (1995-08-03)
Re: Origins of == in C bas@phys.uva.nl (Bas V._de Bakker) (1995-08-09)
| List of all articles for this month |

Newsgroups: comp.compilers
From: David Toland <det@sw.stratus.com>
Keywords: C, design
Organization: Compilers Central
References: 95-07-158
Date: Thu, 3 Aug 1995 15:02:31 GMT

Brendan Gowing <Brendan.Gowing@cs.tcd.ie> writes:
> However, I think that a more intrinsic problem raised by C's "=" and
> "==" is the fact that C grammar treats both as operators in an
> expression. If this were not the case and "=" was constrained to use
> in a statement, then C compilers could spot a lot of the problems that
> you are encountering.


I've long felt that the real problem is not that "=" is an operator
and therefore useable in arbitrary expressions. Indeed that is part
of the expressive *power* of C and C++.


Instead, I think much more type safety and syntax checking could have been
had at the cost of a little more text in some places by having a boolean
type as part of the language, and requiring a boolean expression in
the test expression of for, while, if, and do...while constructs.
In this way,


if (a = b)


Would be diagnosed as an invalid construct. If what was really intended
was to assign b to a and test for 0, then it wouild be coded as:


if ((a = b) != 0)


  (or)


if ((bool)(a = b))


I am also assuming that a boolean type would be an integer type, of size
typically the same as a char. Whether it could hold nonzero values other
than 1 could be implementation-defined, but a boolean operator would only
return a 0 or a 1.


Obviously it is too late to add that to the language at this point, but
I believe that had it been there from the beginning it would have eliminated
an all too common source of error without crippling the language.


--
det@phlan.sw.stratus.com |
(Dave Toland) |
[Wizard C, the predecessor of Turbo C, had an optional warning whenever
the top-level operator in an if or while was = and it was quite useful.
Being a lazy typist, I took to writing if(!!(a = b)) { ... -John]


--


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.