Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Re: K&R beginner code query (http://www.velocityreviews.com/forums/t754741-re-k-and-r-beginner-code-query.html)

Morris Keesan 10-10-2011 06:20 PM

Re: K&R beginner code query
 
On Mon, 10 Oct 2011 14:08:44 -0400, Anthony <carranty@gmail.com> wrote:

> if (c == ' ' || c == '\n' || c = '\t')

....
> My compiler says
>
> line14 : lvalue required as left operand of assignment

....
> The above is copied straight out of the book yet it still won't compile


It was copied out of the book, but you've introduced an error in your
copying. Look closely at the last (intended) comparison on that line.
This is something that many beginning C programmers have problems with.
Note the difference between the assignment operator ( = ) and the
equality operator ( == ). Even some of us who have been programming
in C for decades occasionally produce typos like this, which is why some
people prefer to write comparisons like

if (' ' == c)
instead of
if (c == ' ')

because if the == is mistyped as =, the former will cause the compilation
error you've observed, while the latter will accidentally change the
value of c, and produce debugging headaches.
--
Morris Keesan -- mkeesan@post.harvard.edu

jacob navia 10-10-2011 06:58 PM

Re: K&R beginner code query
 
Le 10/10/11 20:20, Morris Keesan a écrit :
> On Mon, 10 Oct 2011 14:08:44 -0400, Anthony <carranty@gmail.com> wrote:
>
>> if (c == ' ' || c == '\n' || c = '\t')

> ...
>> My compiler says
>>
>> line14 : lvalue required as left operand of assignment

> ...
>> The above is copied straight out of the book yet it still won't compile

>
> It was copied out of the book, but you've introduced an error in your
> copying. Look closely at the last (intended) comparison on that line.
> This is something that many beginning C programmers have problems with.
> Note the difference between the assignment operator ( = ) and the
> equality operator ( == ). Even some of us who have been programming
> in C for decades occasionally produce typos like this, which is why some
> people prefer to write comparisons like
>


I remember the discussion about C digraphs that we had in this group
where I proposed to replace the assignment by a left arrow, and the
equality by the equals sign.

That way all problems like this would disappear, but that was too much
for many conservative people here...



Keith Thompson 10-10-2011 08:38 PM

Re: K&R beginner code query
 
"Morris Keesan" <mkeesan@post.harvard.edu> writes:
> On Mon, 10 Oct 2011 14:08:44 -0400, Anthony <carranty@gmail.com> wrote:
>
>> if (c == ' ' || c == '\n' || c = '\t')

> ...
>> My compiler says
>>
>> line14 : lvalue required as left operand of assignment

> ...
>> The above is copied straight out of the book yet it still won't compile

>
> It was copied out of the book, but you've introduced an error in your
> copying. Look closely at the last (intended) comparison on that line.
> This is something that many beginning C programmers have problems with.
> Note the difference between the assignment operator ( = ) and the
> equality operator ( == ). Even some of us who have been programming
> in C for decades occasionally produce typos like this, which is why some
> people prefer to write comparisons like
>
> if (' ' == c)
> instead of
> if (c == ' ')
>
> because if the == is mistyped as =, the former will cause the compilation
> error you've observed, while the latter will accidentally change the
> value of c, and produce debugging headaches.


And some of us find that kind of backwards comparison quite ugly. (Yes,
I know it's semantically identical; that's not the point.)

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Ike Naar 10-10-2011 10:07 PM

Re: K&R beginner code query
 
On 2011-10-10, Keith Thompson <kst-u@mib.org> wrote:
> "Morris Keesan" <mkeesan@post.harvard.edu> writes:
>> [...] . Even some of us who have been programming
>> in C for decades occasionally produce typos like this, which is why some
>> people prefer to write comparisons like
>>
>> if (' ' == c)
>> instead of
>> if (c == ' ')
>>
>> because if the == is mistyped as =, the former will cause the compilation
>> error you've observed, while the latter will accidentally change the
>> value of c, and produce debugging headaches.

>
> And some of us find that kind of backwards comparison quite ugly. (Yes,
> I know it's semantically identical; that's not the point.)


Equality is symmetric; symmetry is a beautiful property.
So equalities cannot be backwards, and they cannot be ugly ;-)

Geoff 10-10-2011 11:22 PM

Re: K&R beginner code query
 
On Mon, 10 Oct 2011 22:07:41 +0000 (UTC), Ike Naar
<ike@iceland.freeshell.org> wrote:

>Equality is symmetric; symmetry is a beautiful property.
>So equalities cannot be backwards, and they cannot be ugly ;-)


Zen! :-) Ahhh! Bliss.

Keith Thompson 10-11-2011 12:17 AM

Re: K&R beginner code query
 
Ike Naar <ike@iceland.freeshell.org> writes:
> On 2011-10-10, Keith Thompson <kst-u@mib.org> wrote:
>> "Morris Keesan" <mkeesan@post.harvard.edu> writes:
>>> [...] . Even some of us who have been programming
>>> in C for decades occasionally produce typos like this, which is why some
>>> people prefer to write comparisons like
>>>
>>> if (' ' == c)
>>> instead of
>>> if (c == ' ')
>>>
>>> because if the == is mistyped as =, the former will cause the compilation
>>> error you've observed, while the latter will accidentally change the
>>> value of c, and produce debugging headaches.

>>
>> And some of us find that kind of backwards comparison quite ugly. (Yes,
>> I know it's semantically identical; that's not the point.)

>
> Equality is symmetric; symmetry is a beautiful property.
> So equalities cannot be backwards, and they cannot be ugly ;-)


I'm sure I could show you plenty of examples of things that are both
symmetric and ugly. 8-)}

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Morris Keesan 10-12-2011 02:55 PM

Re: K&R beginner code query
 
On Mon, 10 Oct 2011 16:38:39 -0400, Keith Thompson <kst-u@mib.org> wrote:

> "Morris Keesan" <mkeesan@post.harvard.edu> writes:

....
>> some people prefer to write comparisons like
>>
>> if (' ' == c)
>> instead of
>> if (c == ' ')
>>
>> because if the == is mistyped as =, the former will cause the
>> compilation
>> error you've observed, while the latter will accidentally change the
>> value of c, and produce debugging headaches.

>
> And some of us find that kind of backwards comparison quite ugly. (Yes,
> I know it's semantically identical; that's not the point.)


I don't like it, myself. I don't find it ugly, just awkward.
But I thought it was worth mentioning, to an obvious beginner.
--
Morris Keesan -- mkeesan@post.harvard.edu


All times are GMT. The time now is 06:45 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57