Would this cause undefined behavior please?

Discussion in 'C Programming' started by rayw, Aug 18, 2008.

  1. rayw

    rayw Guest

    int x,y,z;

    x=y=z=1;

    z=++x||++y && ++z;

    My understanding is that the order of things here are:

    ++x = x = 2

    As x is 'true', the ++y doesn't operate

    To evaluate &&, ++z operates, setting z to 2.

    true && true, and that assigns 1 to z.

    Is this right please?
    rayw, Aug 18, 2008
    #1
    1. Advertising

  2. rayw

    rayw Guest


    >      z=++x||++y && ++z;
    > means the same thing as:
    >      z = ++x || (++y && ++z);
    >
    > To evaluate the result of the || operator,
    > (++x) is evaluated first, like you said,
    > but after that, there is nothing more to do.
    >
    > (++y && ++z) is not evaluated.


    Of course! Many thanks.
    rayw, Aug 18, 2008
    #2
    1. Advertising

  3. rayw

    Guest

    pete <> wrote:
    >
    > (++y && ++z) is not evaluated.


    Which is good because, if it were, you (the OP, not pete) would be
    trying to set the value of z twice in the same expression without a
    sequence point between them, which is undefined behavior, just like:

    z = ++z;
    --
    Larry Jones

    I just can't identify with that kind of work ethic. -- Calvin
    , Aug 22, 2008
    #3
    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. Mantorok Redgormor
    Replies:
    70
    Views:
    1,724
    Dan Pop
    Feb 17, 2004
  2. Martin
    Replies:
    7
    Views:
    679
    Chris Torek
    Dec 9, 2004
  3. John Smith

    What would cause this problem? CPU usage 100%

    John Smith, Jul 15, 2005, in forum: C Programming
    Replies:
    5
    Views:
    315
    Robert Gamble
    Jul 16, 2005
  4. Jim Langston
    Replies:
    16
    Views:
    490
    AnalogFile
    Mar 22, 2006
  5. Would this cause a memory leak?

    , Aug 1, 2007, in forum: C Programming
    Replies:
    11
    Views:
    604
Loading...

Share This Page