Question regarding the short circuit behavior

Discussion in 'C Programming' started by somenath, Dec 14, 2007.

  1. somenath

    somenath Guest

    Hi All,
    I have one question regarding the bellow mentioned code


    #include<stdio.h>

    int main(void)
    {
    int x = 0;
    int y = 0;
    if ( x++ && ++y)
    {
    ++x;
    }
    printf("%d %d\n",x, y);
    return 0;
    }

    Output of the program
    1 0

    But my understanding is the output of the program should be 2 1. I
    would like to explain my understanding.

    Initially x is 0 . in "if ( x++ && ++y)" x will be 1 as && introduce
    sequence point that's why x will have value 1 . Then as per rule C
    compiler will not stop evaluating the "if ( x++ && ++y)" as it does
    not know the result of & so value of y will be 1
    ..Now "if ( x++ && ++y)" becomes "if ( 1 &&1 )" is if(1) . So ++x will
    be executed .So the final value of x and y is 2,1 .But it is not so .

    Where am I going wrong ?


    Regards,
    Somenath
     
    somenath, Dec 14, 2007
    #1
    1. Advertising

  2. somenath

    user923005 Guest

    Re: Question regarding the short circuit behavior

    On Dec 13, 9:56 pm, somenath <> wrote:
    > Hi All,
    > I have one question regarding the bellow mentioned code
    >
    > #include<stdio.h>
    >
    > int main(void)
    > {
    > int x = 0;
    > int y = 0;
    > if ( x++ && ++y)
    > {
    > ++x;
    > }
    > printf("%d %d\n",x, y);
    > return 0;
    >
    > }
    >
    > Output of the program
    > 1 0
    >
    > But my understanding is the output of the program should be 2 1. I
    > would like to explain my understanding.
    >
    > Initially x is 0 . in "if ( x++ && ++y)" x will be 1 as && introduce
    > sequence point that's why x will have value 1 . Then as per rule C
    > compiler will not stop evaluating the "if ( x++ && ++y)" as it does
    > not know the result of & so value of y will be 1
    > .Now "if ( x++ && ++y)" becomes "if ( 1 &&1 )" is if(1) . So ++x will
    > be executed .So the final value of x and y is 2,1 .But it is not so .
    >
    > Where am I going wrong ?


    x is evaluated as zero and then incremented. That is the meaning of x+
    + as opposed to ++x.
    Is it clear?

    > Regards,
    > Somenath
     
    user923005, Dec 14, 2007
    #2
    1. Advertising

  3. somenath

    somenath Guest

    Re: Question regarding the short circuit behavior

    On Dec 14, 11:05 am, user923005 <> wrote:
    > On Dec 13, 9:56 pm, somenath <> wrote:
    >
    >
    >
    >
    >
    > > Hi All,
    > > I have one question regarding the bellow mentioned code

    >
    > > #include<stdio.h>

    >
    > > int main(void)
    > > {
    > > int x = 0;
    > > int y = 0;
    > > if ( x++ && ++y)
    > > {
    > > ++x;
    > > }
    > > printf("%d %d\n",x, y);
    > > return 0;

    >
    > > }

    >
    > > Output of the program
    > > 1 0

    >
    > > But my understanding is the output of the program should be 2 1. I
    > > would like to explain my understanding.

    >
    > > Initially x is 0 . in "if ( x++ && ++y)" x will be 1 as && introduce
    > > sequence point that's why x will have value 1 . Then as per rule C
    > > compiler will not stop evaluating the "if ( x++ && ++y)" as it does
    > > not know the result of & so value of y will be 1
    > > .Now "if ( x++ && ++y)" becomes "if ( 1 &&1 )" is if(1) . So ++x will
    > > be executed .So the final value of x and y is 2,1 .But it is not so .

    >
    > > Where am I going wrong ?

    >
    > x is evaluated as zero and then incremented. That is the meaning of x+
    > + as opposed to ++x.
    > Is it clear?


    My question is why x is evaluated as zero ? Because compiler will stop
    evaluating one expression only in sequence point . Is this not true? I
    think I missing the concept how "if" statement work
     
    somenath, Dec 14, 2007
    #3
  4. somenath

    CBFalconer Guest

    somenath wrote:
    >
    > #include<stdio.h>
    > int main(void) {
    > int x = 0;
    > int y = 0;
    >
    > if (x++ && ++y) ++x;
    > printf("%d %d\n",x, y);
    > return 0;
    > }
    >
    > Output of the program
    > 1 0
    >
    > But my understanding is the output of the program should be 2 1. I
    > would like to explain my understanding.
    >
    > Initially x is 0 . in "if ( x++ && ++y)" x will be 1 as && introduce
    > sequence point that's why x will have value 1 . Then as per rule C
    > compiler will not stop evaluating the "if ( x++ && ++y)" as it does
    > not know the result of & so value of y will be 1
    > .Now "if ( x++ && ++y)" becomes "if ( 1 &&1 )" is if(1) . So ++x will
    > be executed .So the final value of x and y is 2,1 .But it is not so .


    In the if statement, the value of x++ is 0. Thus the condition is
    false and there is no need to test ++y, whose execution is
    skipped. Result, 1 0.

    Change the && to || and you get your expectation.

    --
    Chuck F (cbfalconer at maineline dot net)
    <http://cbfalconer.home.att.net>
    Try the download section.


    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Dec 14, 2007
    #4
  5. somenath

    Ian Collins Guest

    Re: Question regarding the short circuit behavior

    somenath wrote:
    > On Dec 14, 11:05 am, user923005 <> wrote:
    >> On Dec 13, 9:56 pm, somenath <> wrote:


    >>> Hi All,
    >>> I have one question regarding the bellow mentioned code
    >>> #include<stdio.h>
    >>> int main(void)
    >>> {
    >>> int x = 0;
    >>> int y = 0;
    >>> if ( x++ && ++y)
    >>> {
    >>> ++x;
    >>> }
    >>> printf("%d %d\n",x, y);
    >>> return 0;
    >>> }
    >>> Output of the program
    >>> 1 0


    >>> Where am I going wrong ?

    >> x is evaluated as zero and then incremented. That is the meaning of x+
    >> + as opposed to ++x.
    >> Is it clear?

    >
    > My question is why x is evaluated as zero ? Because compiler will stop
    > evaluating one expression only in sequence point . Is this not true? I
    > think I missing the concept how "if" statement work


    It's nothing to do with the if statement. x++ is a post-increment
    operator, the value of x is returned and x is then incremented. If you
    were to write ++x, the pre-increment form, x would be incremented and
    then evaluated as 1.

    --
    Ian Collins.
     
    Ian Collins, Dec 14, 2007
    #5
  6. somenath <> writes:
    > I have one question regarding the bellow mentioned code
    >
    >
    > #include<stdio.h>
    >
    > int main(void)
    > {
    > int x = 0;
    > int y = 0;
    > if ( x++ && ++y)
    > {
    > ++x;
    > }
    > printf("%d %d\n",x, y);
    > return 0;
    > }
    >
    > Output of the program
    > 1 0
    >
    > But my understanding is the output of the program should be 2 1.

    [...]

    No, "1 0" is correct.

    The left operand of the "&&" is "x++", where the original value of x
    is 0. The "x++" causes x to become one, but postfix "++" yields the
    *previous* value of its argument. So the result of "x++" is 0. This
    short-circuits the "&&" operator, so "++y" is not evaluated and "{
    ++x; }" is not executed.

    If you change the condition from
    x++ && ++y
    to
    ++x && ++y
    you'll get the result you expected.

    --
    Keith Thompson (The_Other_Keith) <>
    Looking for software development work in the San Diego area.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Dec 14, 2007
    #6
  7. somenath

    Chris Dollin Guest

    somenath wrote:

    > Hi All,
    > I have one question regarding the bellow mentioned code


    BELOW. One L. Below, underneath. Bellow, shout.

    [Also, said the pedant, the code isn't /mentioned/ below -- it /is/
    below.]

    > #include<stdio.h>
    >
    > int main(void)
    > {
    > int x = 0;
    > int y = 0;
    > if ( x++ && ++y)
    > {
    > ++x;
    > }
    > printf("%d %d\n",x, y);
    > return 0;
    > }
    >
    > Output of the program
    > 1 0
    >
    > But my understanding is the output of the program should be 2 1. I
    > would like to explain my understanding.
    >
    > Initially x is 0 . in "if ( x++ && ++y)" x will be 1 as && introduce
    > sequence point that's why x will have value 1 .


    `x` /starts off/ as zero. The tested value is zero. Hence the condition
    fails. `x` is incremented as a side-effect.

    `x++` and `++x` are different.

    --
    Two-Sides Hedgehog
    "Life is full of mysteries. Consider this one of them." Sinclair, /Babylon 5/
     
    Chris Dollin, Dec 14, 2007
    #7
  8. somenath

    Richard Bos Guest

    Re: Question regarding the short circuit behavior

    Ian Collins <> wrote:

    > somenath wrote:
    > > On Dec 14, 11:05 am, user923005 <> wrote:
    > >> On Dec 13, 9:56 pm, somenath <> wrote:

    >
    > >>> if ( x++ && ++y)


    > >> x is evaluated as zero and then incremented. That is the meaning of x++
    > >> as opposed to ++x.


    Nope, that's too simplistic.

    > > My question is why x is evaluated as zero ? Because compiler will stop
    > > evaluating one expression only in sequence point . Is this not true? I
    > > think I missing the concept how "if" statement work

    >
    > It's nothing to do with the if statement. x++ is a post-increment
    > operator, the value of x is returned and x is then incremented. If you
    > were to write ++x, the pre-increment form, x would be incremented and
    > then evaluated as 1.


    Almost, but not quite. In as simple a situation as the above, that's how
    you can safely pretend it works, but in fact all the Standard requires
    is that:

    for the pre-increment ++i,
    - some time before the next sequence point, i is incremented;
    - this sub-expression evaluates to the _new_ value of i,

    and for the post-increment i++,
    - some time before the next sequence point, i is incremented;
    - this sub-expression evaluates to the _old_ value of i.

    Note that in both cases, the actual increment of the object can happen
    before, after, or even in parallel with the evaluation of the
    expression. This is important to understand why, for example, undefined
    behaviour involving ++ operators need not be restricted to two or three
    values, but can actually crash in reasonable situations.

    Richard
     
    Richard Bos, Dec 14, 2007
    #8
  9. somenath

    Ian Collins Guest

    Re: Question regarding the short circuit behavior

    Richard Bos wrote:
    > Ian Collins <> wrote:
    >
    >> somenath wrote:
    >>> On Dec 14, 11:05 am, user923005 <> wrote:
    >>>> On Dec 13, 9:56 pm, somenath <> wrote:
    >>>>> if ( x++ && ++y)

    >
    >>>> x is evaluated as zero and then incremented. That is the meaning of x++
    >>>> as opposed to ++x.

    >
    > Nope, that's too simplistic.
    >
    >>> My question is why x is evaluated as zero ? Because compiler will stop
    >>> evaluating one expression only in sequence point . Is this not true? I
    >>> think I missing the concept how "if" statement work

    >> It's nothing to do with the if statement. x++ is a post-increment
    >> operator, the value of x is returned and x is then incremented. If you
    >> were to write ++x, the pre-increment form, x would be incremented and
    >> then evaluated as 1.

    >
    > Almost, but not quite. In as simple a situation as the above, that's how
    > you can safely pretend it works,


    You can see I've written a lot of operators in C++, where the simplistic
    version of events is exactly what happens!

    --
    Ian Collins.
     
    Ian Collins, Dec 14, 2007
    #9
  10. somenath

    Guest

    Re: Question regarding the short circuit behavior

    On Dec 14, 10:56 am, somenath <> wrote:
    > Hi All,
    > I have one question regarding the bellow mentioned code
    >
    > #include<stdio.h>
    >
    > int main(void)
    > {
    > int x = 0;
    > int y = 0;
    > if ( x++ && ++y)
    > {
    > ++x;
    > }
    > printf("%d %d\n",x, y);
    > return 0;
    >
    > }
    >
    > Output of the program
    > 1 0
    >
    > But my understanding is the output of the program should be 2 1. I
    > would like to explain my understanding.
    >
    > Initially x is 0 . in "if ( x++ && ++y)" x will be 1 as && introduce
    > sequence point that's why x will have value 1 . Then as per rule C
    > compiler will not stop evaluating the "if ( x++ && ++y)" as it does
    > not know the result of & so value of y will be 1
    > .Now "if ( x++ && ++y)" becomes "if ( 1 &&1 )" is if(1) . So ++x will
    > be executed .So the final value of x and y is 2,1 .But it is not so .
    >
    > Where am I going wrong ?
    >
    > Regards,
    > Somenath

    hi,
    dekh jab tum x++ kar rahe ho tab value increase karegi par jab tum ++y
    karo ge tab value zero hi rahegi kyuki ++y ka matlab hota hai ki phale
    plus karega aur phir y ki value lega isliye y ki value zero hi rahegi
    aur baad mai phir tum ++x kar rahe ho usme bhi yahi hoga x++ karne mai
    initial value ab 1 ho gahi hai par baad mai ++x phele 1rakhega phir
    plus karega matlab(1+0);
    samje isliye final value 2 1 nahi dega.
     
    , Dec 14, 2007
    #10
  11. In article <>,
    somenath <> wrote:

    >Initially x is 0 . in "if ( x++ && ++y)" x will be 1


    x will be 1 after the evaluation after the evaluation of x++. But the
    value of the expression x++ (which is what you're testing) is the
    *old* value of x, so it's 0.

    Try printf("%d\n", x++) to convince yourself.

    -- Richard
    --
    :wq
     
    Richard Tobin, Dec 14, 2007
    #11
  12. somenath

    pete Guest

    Re: Question regarding the short circuit behavior

    somenath wrote:

    > > > Hi All,
    > > > I have one question regarding the bellow mentioned code

    > >
    > > > #include<stdio.h>

    > >
    > > > int main(void)
    > > > {
    > > > int x = 0;
    > > > int y = 0;
    > > > if ( x++ && ++y)
    > > > {
    > > > ++x;
    > > > }
    > > > printf("%d %d\n",x, y);
    > > > return 0;


    > My question is why x is evaluated as zero ?


    (x++) is an expression of object type.
    A useful way for to consider expressions of object type
    is in terms of:
    1 Values
    2 Side Effects

    The value of (x++), is the original value of (x).
    The side effect, is that x gets incremented.

    The value of (++x), is the final value of (x).
    The side effect, is that x gets incremented.

    Whether the increment takes place
    prior to or subsequent to
    the evaluation, is unspecified by the standard.

    These two expressions are interchangable: (x++) and (x++, x - 1)

    --
    pete
     
    pete, Dec 14, 2007
    #12
  13. >>>>> "CD" == Chris Dollin <> writes:

    CD> somenath wrote:
    >> Hi All, I have one question regarding the bellow mentioned code


    CD> BELOW. One L. Below, underneath. Bellow, shout.

    Maybe he's WISHING HE COULD TYPE THE CODE IN ALL CAPS but he realizes
    this is C, not FORTRAN?

    Charlton


    --
    Charlton Wilbur
     
    Charlton Wilbur, Dec 14, 2007
    #13
  14. somenath

    Thad Smith Guest

    Re: Question regarding the short circuit behavior

    pete wrote:

    > The value of (x++), is the original value of (x).
    > The side effect, is that x gets incremented.
    >
    > The value of (++x), is the final value of (x).
    > The side effect, is that x gets incremented.
    >
    > Whether the increment takes place
    > prior to or subsequent to
    > the evaluation, is unspecified by the standard.


    Good explanation.

    > These two expressions are interchangable: (x++) and (x++, x - 1)


    The resulting values are the same, but they are not interchangeable,
    since the second expression introduces a sequence point following the
    increment, which affects whether the following expressions are defined:

    (x++) * x /* undefined */
    (x++, x - 1) * x /* defined for suitable x */


    --
    Thad
     
    Thad Smith, Dec 15, 2007
    #14
  15. Re: Question regarding the short circuit behavior

    On Sat, 15 Dec 2007 09:09:10 -0700, Thad Smith wrote:
    > pete wrote:
    >> These two expressions are interchangable: (x++) and (x++, x - 1)

    >
    > The resulting values are the same, but they are not interchangeable,
    > since the second expression introduces a sequence point following the
    > increment, which affects whether the following expressions are defined:
    >
    > (x++) * x /* undefined */
    > (x++, x - 1) * x /* defined for suitable x */


    The second expression is undefined has well. The comma operator does
    introduce a sequence point, but the final reference to x is not the right
    operand to the comma operator. There is no requirement that the
    multiplication by x happens after the increment. It is equivalent to
    x * (x++, x - 1)
    where it is perhaps easier to see the problem.
     
    Harald van Dijk, Dec 15, 2007
    #15
  16. somenath

    Thad Smith Guest

    Re: Question regarding the short circuit behavior

    Harald van Dijk wrote:
    > On Sat, 15 Dec 2007 09:09:10 -0700, Thad Smith wrote:
    >> pete wrote:
    >>> These two expressions are interchangable: (x++) and (x++, x - 1)

    >> The resulting values are the same, but they are not interchangeable,
    >> since the second expression introduces a sequence point following the
    >> increment, which affects whether the following expressions are defined:
    >>
    >> (x++) * x /* undefined */
    >> (x++, x - 1) * x /* defined for suitable x */

    >
    > The second expression is undefined has well. The comma operator does
    > introduce a sequence point, but the final reference to x is not the right
    > operand to the comma operator. There is no requirement that the
    > multiplication by x happens after the increment. It is equivalent to
    > x * (x++, x - 1)
    > where it is perhaps easier to see the problem.


    Good catch!

    I now think Pete is correct. I see no way to take advantage of the
    sequence point introduced by the comma operator.

    --
    Thad
     
    Thad Smith, Dec 15, 2007
    #16
  17. Re: Question regarding the short circuit behavior

    On Dec 15, 12:53 am, pete wrote:
    > These two expressions are interchangable:
    > (x++) and (x++, x - 1)


    Almost.

    If x is an unsigned short with the value USHRT_MAX,
    and if USHRT_MAX <= INT_MAX, then the former yields
    USHRT_MAX, the latter yields -1. [Also, cf sizeof]

    Similarly for unsigned char.

    There are also potential differences if x is volatile.

    --
    Peter
     
    Peter Nilsson, Dec 16, 2007
    #17
  18. somenath

    pete Guest

    Re: Question regarding the short circuit behavior

    Peter Nilsson wrote:
    >
    > On Dec 15, 12:53 am, pete wrote:
    > > These two expressions are interchangable:
    > > (x++) and (x++, x - 1)

    >
    > Almost.
    >
    > If x is an unsigned short


    My statement was intended to be in the context
    of the quoted code, which you snipped:

    > > > int x = 0;



    The expressions are also not interchangable
    if x is a macro with side effects.

    --
    pete
     
    pete, Dec 16, 2007
    #18
    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. Phil...
    Replies:
    10
    Views:
    738
    Joona I Palaste
    Oct 23, 2003
  2. Phil...
    Replies:
    1
    Views:
    979
    Thomas G. Marshall
    Oct 22, 2003
  3. JS#
    Replies:
    5
    Views:
    3,539
    Roedy Green
    Nov 21, 2005
  4. webposter
    Replies:
    2
    Views:
    665
    Peter Shaggy Haywood
    Sep 14, 2004
  5. Michael Jørgensen

    boolean short circuit

    Michael Jørgensen, Sep 22, 2005, in forum: C Programming
    Replies:
    5
    Views:
    335
    Jack Klein
    Sep 23, 2005
Loading...

Share This Page