Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: K&R beginner code query

Reply
Thread Tools

Re: K&R beginner code query

 
 
Morris Keesan
Guest
Posts: n/a
 
      10-10-2011
On Mon, 10 Oct 2011 14:08:44 -0400, Anthony <(E-Mail Removed)> 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 -- http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
jacob navia
Guest
Posts: n/a
 
      10-10-2011
Le 10/10/11 20:20, Morris Keesan a écrit :
> On Mon, 10 Oct 2011 14:08:44 -0400, Anthony <(E-Mail Removed)> 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...


 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      10-10-2011
"Morris Keesan" <(E-Mail Removed)> writes:
> On Mon, 10 Oct 2011 14:08:44 -0400, Anthony <(E-Mail Removed)> 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) (E-Mail Removed) <http://www.ghoti.net/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Ike Naar
Guest
Posts: n/a
 
      10-10-2011
On 2011-10-10, Keith Thompson <(E-Mail Removed)> wrote:
> "Morris Keesan" <(E-Mail Removed)> 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
 
Reply With Quote
 
Geoff
Guest
Posts: n/a
 
      10-10-2011
On Mon, 10 Oct 2011 22:07:41 +0000 (UTC), Ike Naar
<(E-Mail Removed)> wrote:

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


Zen! Ahhh! Bliss.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-11-2011
Ike Naar <(E-Mail Removed)> writes:
> On 2011-10-10, Keith Thompson <(E-Mail Removed)> wrote:
>> "Morris Keesan" <(E-Mail Removed)> 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. }

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Morris Keesan
Guest
Posts: n/a
 
      10-12-2011
On Mon, 10 Oct 2011 16:38:39 -0400, Keith Thompson <(E-Mail Removed)> wrote:

> "Morris Keesan" <(E-Mail Removed)> 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 -- (E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Beginner's Beginner william nelson Ruby 7 04-11-2011 11:23 PM
beginner question: feeding a query string into an Xpath expression xerj ASP .Net 2 05-08-2006 01:17 PM
No Class at ALL!!! beginner/beginner question =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?= ASP .Net 7 02-03-2005 02:47 PM
Problem getting a DropDownList populated by query(beginner) =?Utf-8?B?TWljcm9Nb3Ro?= ASP .Net 0 01-19-2005 01:01 PM
Tutorial for beginner/ Tutorial voor beginner Rensjuh C++ 7 09-02-2004 12:41 AM



Advertisments