comparison

Discussion in 'C Programming' started by Ajay, Jul 19, 2006.

  1. Ajay

    Ajay Guest

    What is the difference betweem

    if (x == 1)

    AND

    if (1==x)

    -Ajay
    Ajay, Jul 19, 2006
    #1
    1. Advertising

  2. Ajay said:

    > What is the difference betweem
    >
    > if (x == 1)
    >
    > AND
    >
    > if (1==x)


    The reason some people (including myself) prefer the second is that the
    error of mistyping = instead of == will be picked up by the compiler.
    Consider:

    if(x = 1)

    and

    if(1 = x)

    The former is legal C, but not what you wanted (or you would not have
    bothered with the if). The second is not legal C, so the compiler will
    diagnose it.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
    Richard Heathfield, Jul 19, 2006
    #2
    1. Advertising

  3. Ajay

    Chris Dollin Guest

    Ajay wrote:

    > What is the difference betweem
    >
    > if (x == 1)
    >
    > AND
    >
    > if (1==x)


    The operands to `==` are in opposite orders.

    Some people feel that the second form gives some immunity to
    the `x = 1` problem.

    Other people feel that the second order is sufficiently unnatural
    that it will /cause/ problems.

    It's likely that the best tactic is to pick one of the forms,
    and use that consistently though your workgroup or compatibly
    with your audience.

    You are invited to guess my opinion from the subtle hints in
    this post.

    --
    Chris "subtle, like a sledgehammer" Dollin
    A rock is not a fact. A rock is a rock.
    Chris Dollin, Jul 19, 2006
    #3
  4. Ajay

    Richard Bos Guest

    "Ajay" <> wrote:

    > What is the difference betweem
    >
    > if (x == 1)
    >
    > AND
    >
    > if (1==x)


    The latter will cause you to think that you'll be automatically warned
    if you accidentally type = instead of ==, which means that you'll have a
    much harder time finding the bug when you inevitably write if (y=x).

    Richard
    Richard Bos, Jul 19, 2006
    #4
  5. Ajay wrote:
    > What is the difference betweem
    >
    > if (x == 1)
    >
    > AND
    >
    > if (1==x)


    Apart from typography, none.
    Since my compiler will warn me about
    if (x = 1)
    and my normal natural language way of expressing the comparison is
    if x is equal to 1
    I prefer the first.

    People who have turned off their diagnostics (a bad thing), or use a
    less helpful compiler, or who think of 1 as a variable, as in
    if 1 is equal to x,
    will prefer the second.
    Martin Ambuhl, Jul 19, 2006
    #5
  6. "Ajay" <> writes:
    > What is the difference betweem
    >
    > if (x == 1)
    >
    > AND
    >
    > if (1==x)


    One is ugly. The other is not. (There's no semantic difference.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Jul 19, 2006
    #6
  7. Richard Bos said:

    > "Ajay" <> wrote:
    >
    >> What is the difference betweem
    >>
    >> if (x == 1)
    >>
    >> AND
    >>
    >> if (1==x)

    >
    > The latter will cause you to think that you'll be automatically warned
    > if you accidentally type = instead of ==,


    Wrong. It will cause you to *know* that you'll be automatically warned if
    you accidentally type = instead of == when comparing a constant and an
    lvalue.

    > which means that you'll have a
    > much harder time finding the bug when you inevitably write if (y=x).


    No. I agree that such a bug is *hard* to find if your compiler doesn't
    notice it, but just because a related error is *easier* to find, that does
    not make /this/ error *harder* to find than it was before.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
    Richard Heathfield, Jul 19, 2006
    #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. LV

    .Net Comparison

    LV, Aug 27, 2003, in forum: ASP .Net
    Replies:
    4
    Views:
    1,401
    William Ryan
    Aug 31, 2003
  2. bettervssremoting
    Replies:
    0
    Views:
    1,858
    bettervssremoting
    Mar 1, 2005
  3. Anand P Paralkar
    Replies:
    2
    Views:
    9,379
    Srinivasan Venkataramanan
    Aug 4, 2003
  4. Just an Illusion

    VHDL revisions comparison

    Just an Illusion, Jun 30, 2004, in forum: VHDL
    Replies:
    1
    Views:
    557
    Arie Zychlinski
    Jun 30, 2004
  5. Deepu
    Replies:
    1
    Views:
    236
    ccc31807
    Feb 7, 2011
Loading...

Share This Page