![]() |
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 |
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... |
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" |
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 ;-) |
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. |
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" |
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.