Velocity Reviews > Question regarding the short circuit behavior

# Question regarding the short circuit behavior

somenath
Guest
Posts: n/a

 12-14-2007
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

user923005
Guest
Posts: n/a

 12-14-2007
On Dec 13, 9:56 pm, somenath <(E-Mail Removed)> 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

somenath
Guest
Posts: n/a

 12-14-2007
On Dec 14, 11:05 am, user923005 <(E-Mail Removed)> wrote:
> On Dec 13, 9:56 pm, somenath <(E-Mail Removed)> 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

CBFalconer
Guest
Posts: n/a

 12-14-2007
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>

--
Posted via a free Usenet account from http://www.teranews.com

Ian Collins
Guest
Posts: n/a

 12-14-2007
somenath wrote:
> On Dec 14, 11:05 am, user923005 <(E-Mail Removed)> wrote:
>> On Dec 13, 9:56 pm, somenath <(E-Mail Removed)> 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.

Keith Thompson
Guest
Posts: n/a

 12-14-2007
somenath <(E-Mail Removed)> 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) <(E-Mail Removed)>
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"

Chris Dollin
Guest
Posts: n/a

 12-14-2007
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/

Richard Bos
Guest
Posts: n/a

 12-14-2007
Ian Collins <(E-Mail Removed)> wrote:

> somenath wrote:
> > On Dec 14, 11:05 am, user923005 <(E-Mail Removed)> wrote:
> >> On Dec 13, 9:56 pm, somenath <(E-Mail Removed)> 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

Ian Collins
Guest
Posts: n/a

 12-14-2007
Richard Bos wrote:
> Ian Collins <(E-Mail Removed)> wrote:
>
>> somenath wrote:
>>> On Dec 14, 11:05 am, user923005 <(E-Mail Removed)> wrote:
>>>> On Dec 13, 9:56 pm, somenath <(E-Mail Removed)> 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.

karancuteboy277@gmail.com
Guest
Posts: n/a

 12-14-2007
On Dec 14, 10:56 am, somenath <(E-Mail Removed)> 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.