Re: K&R beginner code query

Discussion in 'C Programming' started by Morris Keesan, Oct 10, 2011.

  1. On Mon, 10 Oct 2011 14:08:44 -0400, Anthony <> 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 --
     
    Morris Keesan, Oct 10, 2011
    #1
    1. Advertising

  2. Morris Keesan

    jacob navia Guest

    Le 10/10/11 20:20, Morris Keesan a écrit :
    > On Mon, 10 Oct 2011 14:08:44 -0400, Anthony <> 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...
     
    jacob navia, Oct 10, 2011
    #2
    1. Advertising

  3. "Morris Keesan" <> writes:
    > On Mon, 10 Oct 2011 14:08:44 -0400, Anthony <> 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) <http://www.ghoti.net/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Oct 10, 2011
    #3
  4. Morris Keesan

    Ike Naar Guest

    On 2011-10-10, Keith Thompson <> wrote:
    > "Morris Keesan" <> 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 ;-)
     
    Ike Naar, Oct 10, 2011
    #4
  5. Morris Keesan

    Geoff Guest

    On Mon, 10 Oct 2011 22:07:41 +0000 (UTC), Ike Naar
    <> wrote:

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


    Zen! :) Ahhh! Bliss.
     
    Geoff, Oct 11, 2011
    #5
  6. Ike Naar <> writes:
    > On 2011-10-10, Keith Thompson <> wrote:
    >> "Morris Keesan" <> 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. :cool:}

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Oct 11, 2011
    #6
  7. On Mon, 10 Oct 2011 16:38:39 -0400, Keith Thompson <> wrote:

    > "Morris Keesan" <> 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 --
     
    Morris Keesan, Oct 12, 2011
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?TWljcm9Nb3Ro?=

    Problem getting a DropDownList populated by query(beginner)

    =?Utf-8?B?TWljcm9Nb3Ro?=, Jan 19, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    341
    =?Utf-8?B?TWljcm9Nb3Ro?=
    Jan 19, 2005
  2. =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=

    No Class at ALL!!! beginner/beginner question

    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=, Feb 2, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    593
    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=
    Feb 3, 2005
  3. xerj
    Replies:
    2
    Views:
    394
  4. Rensjuh
    Replies:
    7
    Views:
    980
    Mabden
    Sep 2, 2004
  5. william nelson

    Beginner's Beginner

    william nelson, Apr 11, 2011, in forum: Ruby
    Replies:
    7
    Views:
    223
    7stud --
    Apr 12, 2011
Loading...

Share This Page