Did you know?

Discussion in 'C Programming' started by Vijay Kumar R Zanvar, Sep 29, 2003.

  1. Did you know that the following if statement is semantically
    right, but could logically be wrong?

    if ( i = 2 )
    {
    /* do something */
    }

    Tip:

    The compiler may only produce a waring: "Possibly incorrect
    assignment", but we may ignore it. To avoid such a mistake,
    just reverse the two identifiers.

    if ( 2 == i )
    {
    /* do something */
    }

    If '==' is replaced by an '=', the compiler will give an error
    saying "Lvalue required".



    --
    "When you will be pleased to dine, Mr. Homles?" asked Mrs Hudson,
    the landlady, curiosly.
    "At seven thirty, the day after tomorrow." said he invovled in his work.
    Vijay Kumar R Zanvar, Sep 29, 2003
    #1
    1. Advertising

  2. Vijay Kumar R Zanvar <> scribbled the following:
    > Did you know that the following if statement is semantically
    > right, but could logically be wrong?


    > if ( i = 2 )
    > {
    > /* do something */
    > }


    > Tip:


    > The compiler may only produce a waring: "Possibly incorrect
    > assignment", but we may ignore it. To avoid such a mistake,
    > just reverse the two identifiers.


    > if ( 2 == i )
    > {
    > /* do something */
    > }


    > If '==' is replaced by an '=', the compiler will give an error
    > saying "Lvalue required".


    Yes, we did know all this. But thanks for letting us know anyway.

    --
    /-- Joona Palaste () ---------------------------\
    | Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
    | http://www.helsinki.fi/~palaste W++ B OP+ |
    \----------------------------------------- Finland rules! ------------/
    "I am looking for myself. Have you seen me somewhere?"
    - Anon
    Joona I Palaste, Sep 29, 2003
    #2
    1. Advertising

  3. "Joona I Palaste" <> wrote in message
    news:bl8kf8$qlp$...
    > Vijay Kumar R Zanvar <> scribbled the following:


    ^^^^^^^^

    Did I scribble?


    > > Did you know that the following if statement is semantically
    > > right, but could logically be wrong?


    [...]

    > > If '==' is replaced by an '=', the compiler will give an error
    > > saying "Lvalue required".

    >
    > Yes, we did know all this. But thanks for letting us know anyway.
    >
    > --
    > /-- Joona Palaste () ---------------------------\


    I suppose it would be fun working with you, Mr. Palaste. You must be a
    comedian!!

    --
    "We approached the case, you remember Watson," said
    Mr. Homles, "with an absolutely blank mind, which is
    always an advantage."
    Vijay Kumar R Zanvar, Sep 29, 2003
    #3
  4. In article <bl8jh5$94pf0$-berlin.de>, Vijay Kumar R Zanvar wrote:
    > Did you know that the following if statement is semantically
    > right, but could logically be wrong?
    >
    > if ( i = 2 )
    > {
    > /* do something */
    > }



    Yes, it's a well known fact and it's in the FAQ:

    http://www.eskimo.com/~scs/C-faq/q17.4.html


    --
    Andreas Kähäri
    Andreas Kahari, Sep 29, 2003
    #4
  5. Vijay Kumar R Zanvar

    Noah Roberts Guest

    Vijay Kumar R Zanvar wrote:
    > if ( 2 == i )
    > {
    > /* do something */
    > }
    >


    What if 2 != i but i == 2, ever thought of that?

    BTW, did you know that the above code doesn't do anything even if 2 ==
    i? I wonder what happens if i == 2 though...

    NR
    Noah Roberts, Sep 29, 2003
    #5
  6. In article <bl8jh5$94pf0$-berlin.de>,
    Vijay Kumar R Zanvar <> wrote:

    > Did you know that the following if statement is semantically
    > right, but could logically be wrong?
    >
    > if ( i = 2 )
    > {
    > /* do something */
    > }


    Did you know that the following if statement is semantically
    right, but could logically be wrong?

    if ( i = k )
    {
    /* do something */
    }

    --
    Göran Larsson http://www.mitt-eget.com/
    Goran Larsson, Sep 29, 2003
    #6
  7. "Noah Roberts" <> schrieb im Newsbeitrag
    news:bl8poi$6v6$...
    > Vijay Kumar R Zanvar wrote:
    > > if ( 2 == i )
    > > {
    > > /* do something */
    > > }
    > >

    >
    > What if 2 != i but i == 2, ever thought of that?
    >
    > BTW, did you know that the above code doesn't do anything even if 2 ==
    > i? I wonder what happens if i == 2 though...
    >


    What, if you replace "==" with "is equal to" as well as "!=" with "is
    unequal to" and "i" with "the value contained in a variable named i" ?
    Robert
    Robert Stankowic, Sep 29, 2003
    #7
  8. Vijay Kumar R Zanvar

    Ravi Guest

    On Mon, 29 Sep 2003 08:30:12 GMT, lid (Goran
    Larsson) wrote:

    >Did you know that the following if statement is semantically
    >right, but could logically be wrong?
    >
    >if ( i = k )
    >{
    > /* do something */
    >}


    On second thoughts it could be correct. If you want the loop
    to execute with i assigned to the value of k only when k is
    not zero you might as well use that form.

    Sorter than:
    if (k!=0)
    {
    i=k;
    /* do everything ;)*/
    }


    --
    main(){char s[40]="sbwjAeftqbnnfe/dpn!ps!CSbwjACjhgppu/dpn";
    int i;for(i=0;i<39;putchar(s[i++]-1));return 0;}
    Ravi, Sep 29, 2003
    #8
  9. Vijay Kumar R Zanvar

    Jirka Klaue Guest

    Noah Roberts wrote:
    > Vijay Kumar R Zanvar wrote:
    >
    >> if ( 2 == i )
    >> {
    >> /* do something */
    >> }
    >>

    >
    > What if 2 != i but i == 2, ever thought of that?


    Well, ...

    #define i i, 2

    Jirka
    Jirka Klaue, Sep 29, 2003
    #9
  10. Vijay Kumar R Zanvar

    Al Bowers Guest

    Vijay Kumar R Zanvar wrote:

    > "Joona I Palaste" <> wrote in message
    > news:bl8kf8$qlp$...
    >
    >>Vijay Kumar R Zanvar <> scribbled the following:

    >
    >
    > ^^^^^^^^
    >
    > Did I scribble?
    >
    >
    >
    >>>Did you know that the following if statement is semantically
    >>>right, but could logically be wrong?

    >
    >
    > [...]
    >
    >
    >>>If '==' is replaced by an '=', the compiler will give an error
    >>>saying "Lvalue required".

    >>
    >>Yes, we did know all this. But thanks for letting us know anyway.
    >>
    >>--
    >>/-- Joona Palaste () ---------------------------\

    >
    >
    > I suppose it would be fun working with you, Mr. Palaste. You must be a
    > comedian!!
    >


    Perhaps dry humor given the fact that everyone should know
    this if they read the FAG.
    See question 17.4
    http://www.eskimo.com/~scs/C-faq/q17.4.html
    Al Bowers, Sep 29, 2003
    #10
  11. Vijay Kumar R Zanvar

    Noah Roberts Guest

    Ravi wrote:
    > On Mon, 29 Sep 2003 08:30:12 GMT, lid (Goran
    > Larsson) wrote:
    >
    >
    >>Did you know that the following if statement is semantically
    >>right, but could logically be wrong?
    >>
    >>if ( i = k )
    >>{
    >> /* do something */
    >>}

    > Sorter than:
    > if (k!=0)
    > {
    > i=k;
    > /* do everything ;)*/
    > }
    >
    >


    Except in the first i is always given the value k.

    NR
    Noah Roberts, Sep 29, 2003
    #11
  12. Vijay Kumar R Zanvar

    James Hu Guest

    Joona I Palaste <> wrote in message news:<bl8kf8$qlp$>...
    > Vijay Kumar R Zanvar <> scribbled the following:
    > > Tip:

    >
    > > if ( 2 == i )
    > > {
    > > /* do something */
    > > }

    >
    > > If '==' is replaced by an '=', the compiler will give an error
    > > saying "Lvalue required".

    >
    > Yes, we did know all this. But thanks for letting us know anyway.


    In fact, it is in the C-faq.

    http://www.eskimo.com/~scs/C-faq/q17.4.html

    "Read the C-faq. It's better up here!"

    -- James
    James Hu, Sep 29, 2003
    #12
  13. Vijay Kumar R Zanvar

    Richard Bos Guest

    "Vijay Kumar R Zanvar" <> wrote:

    > Tip:
    >
    > The compiler may only produce a waring: "Possibly incorrect
    > assignment", but we may ignore it. To avoid such a mistake,
    > just reverse the two identifiers.
    >
    > if ( 2 == i )
    > {
    > /* do something */
    > }


    Did you know that this is a stupid trick to rely on, because it makes
    the code less clear, doesn't help you in all too many situations, and
    therefore _will_ bite you sooner or later? At least the real solution,
    which is being aware of the problem, will help you debug your code when
    you _do_ make this mistake.

    Richard
    Richard Bos, Sep 29, 2003
    #13
  14. Vijay Kumar R Zanvar

    Mike Wahler Guest

    "Vijay Kumar R Zanvar" <> wrote in message
    news:bl8jh5$94pf0$-berlin.de...
    > Did you know


    Of course we know.

    >that the following if statement is semantically
    > right, but could logically be wrong?
    >
    > if ( i = 2 )
    > {
    > /* do something */
    > }
    >
    > Tip:
    >
    > The compiler may only produce a waring: "Possibly incorrect
    > assignment", but we may ignore it. To avoid such a mistake,
    > just reverse the two identifiers.
    >
    > if ( 2 == i )
    > {
    > /* do something */
    > }
    >
    > If '==' is replaced by an '=', the compiler will give an error
    > saying "Lvalue required".


    This issue has been discussed many, many, many times
    here, if you'd taken the time to check old posts.
    Some folks use the 2 == i form, others don't.

    Also note that sometimes the = (assignment) is indeed
    what the coder wants as in e.g.

    char *p;
    if(p = malloc(100))
    /* malloc succeeded */


    -Mike
    Mike Wahler, Sep 29, 2003
    #14
  15. Greetings.

    In article <bl8jh5$94pf0$-berlin.de>, Vijay Kumar R
    Zanvar wrote:
    > The compiler may only produce a waring: "Possibly incorrect
    > assignment", but we may ignore it. To avoid such a mistake,
    > just reverse the two identifiers.


    Wouldn't a better way of avoiding such a mistake be not to ignore compiler
    warnings in the first place?

    --
    _
    _V.-o Tristan Miller [en,(fr,de,ia)] >< Space is limited
    / |`-' -=-=-=-=-=-=-=-=-=-=-=-=-=-=-= <> In a haiku, so it's hard
    (7_\\ http://www.nothingisreal.com/ >< To finish what you
    Tristan Miller, Sep 29, 2003
    #15
  16. Vijay Kumar R Zanvar

    Ashish Guest

    "Vijay Kumar R Zanvar" <> wrote in message
    news:bl8jh5$94pf0$-berlin.de...
    > Did you know that the following if statement is semantically
    > right, but could logically be wrong?
    >
    > if ( i = 2 )
    > {
    > /* do something */
    > }
    >
    > Tip:
    >
    > The compiler may only produce a waring: "Possibly incorrect
    > assignment", but we may ignore it. To avoid such a mistake,
    > just reverse the two identifiers.
    >
    > if ( 2 == i )
    > {
    > /* do something */
    > }
    >
    > If '==' is replaced by an '=', the compiler will give an error
    > saying "Lvalue required".
    >
    >
    >
    > --
    > "When you will be pleased to dine, Mr. Homles?" asked Mrs Hudson,
    > the landlady, curiosly.
    > "At seven thirty, the day after tomorrow." said he invovled in his work.
    >


    Are you still living in the 19th century? This has been discussed millions
    of times.
    Ashish, Sep 29, 2003
    #16
  17. Richard Bos wrote:

    > "Vijay Kumar R Zanvar" <> wrote:
    >
    >> Tip:
    >>
    >> The compiler may only produce a waring: "Possibly incorrect
    >> assignment", but we may ignore it. To avoid such a mistake,
    >> just reverse the two identifiers.
    >>
    >> if ( 2 == i )
    >> {
    >> /* do something */
    >> }

    >
    > Did you know that this is a stupid trick to rely on,


    It's stupid to rely on it, but not stupid to use it.

    > because it makes
    > the code less clear,


    The expression isn't unclear /at all/, and only an expert could have the
    slightest doubt about it. :)

    > doesn't help you in all too many situations,


    Not casting malloc doesn't help you in all too many situations, either (such
    as in code where malloc is not used, for example). That doesn't mean I'm
    about to start casting malloc, though.

    > and
    > therefore _will_ bite you sooner or later?


    Nope. It doesn't bite, as long as you don't rely on it. It's just another
    tool in the box.

    > At least the real solution,
    > which is being aware of the problem, will help you debug your code when
    > you _do_ make this mistake.


    It is important to be aware of the problem. That doesn't mean you shouldn't
    use the compiler to help you avoid it on occasion.

    --
    Richard Heathfield :
    "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    K&R answers, C books, etc: http://users.powernet.co.uk/eton
    Richard Heathfield, Sep 29, 2003
    #17
  18. Vijay Kumar R Zanvar

    Neil Cerutti Guest

    In article <bl96fi$9j3bs$-berlin.de>, Al
    Bowers wrote:
    > Vijay Kumar R Zanvar wrote:
    >
    >> "Joona I Palaste" <> wrote in message
    >> news:bl8kf8$qlp$...
    >>
    >>>Vijay Kumar R Zanvar <> scribbled the following:

    >> ^^^^^^^^
    >>
    >> Did I scribble?
    >>>>Did you know that the following if statement is semantically
    >>>>right, but could logically be wrong?

    >>
    >> [...]
    >>>Yes, we did know all this. But thanks for letting us know anyway.
    >>>
    >>>--

    >> I suppose it would be fun working with you, Mr. Palaste. You
    >> must be a comedian!!

    >
    > Perhaps dry humor given the fact that everyone should know
    > this if they read the FAG.


    The what!?

    > See question 17.4
    > http://www.eskimo.com/~scs/C-faq/q17.4.html


    Oh.

    I find it's useful to write QAF backwards to how it's usually
    written (Questions Asked Frequently) to avoid this type of
    error. That way, if you miswrite G instead of Q, it will be
    meaningless instead of sort of amusing. ;-)

    --
    Neil Cerutti
    Neil Cerutti, Sep 29, 2003
    #18
  19. Vijay Kumar R Zanvar

    Mike Wahler Guest

    "Neil Cerutti" <> wrote in message
    news:bla4m6$9tk3t$-berlin.de...
    > In article <bl96fi$9j3bs$-berlin.de>, Al
    > Bowers wrote:
    > > Vijay Kumar R Zanvar wrote:
    > >
    > >> "Joona I Palaste" <> wrote in message
    > >> news:bl8kf8$qlp$...
    > >>
    > >>>Vijay Kumar R Zanvar <> scribbled the following:
    > >> ^^^^^^^^
    > >>
    > >> Did I scribble?
    > >>>>Did you know that the following if statement is semantically
    > >>>>right, but could logically be wrong?
    > >>
    > >> [...]
    > >>>Yes, we did know all this. But thanks for letting us know anyway.
    > >>>
    > >>>--
    > >> I suppose it would be fun working with you, Mr. Palaste. You
    > >> must be a comedian!!

    > >
    > > Perhaps dry humor given the fact that everyone should know
    > > this if they read the FAG.

    >
    > The what!?
    >
    > > See question 17.4
    > > http://www.eskimo.com/~scs/C-faq/q17.4.html

    >
    > Oh.
    >
    > I find it's useful to write QAF backwards to how it's usually
    > written (Questions Asked Frequently) to avoid this type of
    > error. That way, if you miswrite G instead of Q, it will be
    > meaningless instead of sort of amusing. ;-)


    It would be a gaffe (GAF) :)

    -Mike
    Mike Wahler, Sep 29, 2003
    #19
  20. Vijay Kumar R Zanvar

    CBFalconer Guest

    Richard Bos wrote:
    > "Vijay Kumar R Zanvar" <> wrote:
    >
    > > The compiler may only produce a waring: "Possibly incorrect
    > > assignment", but we may ignore it. To avoid such a mistake,
    > > just reverse the two identifiers.
    > >
    > > if ( 2 == i )
    > > {
    > > /* do something */
    > > }

    >
    > Did you know that this is a stupid trick to rely on, because it
    > makes the code less clear, doesn't help you in all too many
    > situations, and therefore _will_ bite you sooner or later? At
    > least the real solution, which is being aware of the problem,
    > will help you debug your code when you _do_ make this mistake.


    We have been around this before. Clarity is in the eye of the
    beholder. I consider anything that catches silly errors
    beneficial. This is an error that can escape code review very
    easily.

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
    CBFalconer, Sep 30, 2003
    #20
    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. pek
    Replies:
    0
    Views:
    1,083
  2. trans.  (T. Onoma)

    DID YOU KNOW? -- break can return a value?

    trans. (T. Onoma), Oct 18, 2004, in forum: Ruby
    Replies:
    4
    Views:
    81
    trans. (T. Onoma)
    Oct 18, 2004
  3. Daniel Waite
    Replies:
    2
    Views:
    219
    Daniel Waite
    May 2, 2008
  4. Andries

    I know, I know, I don't know

    Andries, Apr 23, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    222
    Gregory Toomey
    Apr 23, 2004
  5. Mark Lawrence
    Replies:
    63
    Views:
    166
    Robert Kern
    May 17, 2014
Loading...

Share This Page