Re: Origins of == in C

Brendan Gowing <Brendan.Gowing@cs.tcd.ie>
Wed, 26 Jul 1995 09:28:37 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: Brendan Gowing <Brendan.Gowing@cs.tcd.ie>
Keywords: C, design
Organization: Compilers Central
References: 95-07-135
Date: Wed, 26 Jul 1995 09:28:37 GMT

>The distinction between = and == in C has caused me no end of grief. ...


>[My recollection is that they used = for assignment because it's a lot more
>common than comparison. Besides, it's Fortran-compatible. -John]


The moderators comment is correct. As far as I can remember, someone
somewhere along the line found that the number of assignments in a
typical program far outweighed the number of equality checks. This was
acknowledged by C's designers who reasoned that the single "=" for
assignment would save key strokes over the more typical ":=". I cannot
remember who reached this conlcusion, where it was published or which
of C's designers encouraged its adoption.


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.


But if this was the case, code like the following wouldn't work:


ptrType* aFunction (void);
ptrType* myVariable = NULL;
...
{
        if (myVariable = aFunction())
                /* ^^^ assignment =>
                  * myVariable is not NULL, so do
                  * something.
                  */
                printf ("Address returned by aFunction() is: %x\n", myVariable);
        else
                printf ("myVariable is NULL!\n");
}


Bye,


-BG
--


Post a followup to this message

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