Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Question regarding the short circuit behavior

Reply
Thread Tools

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
 
Reply With Quote
 
 
 
 
user923005
Guest
Posts: n/a
 
      12-14-2007
On Dec 13, 9:56 pm, somenath <somenath...@gmail.com> 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


 
Reply With Quote
 
 
 
 
somenath
Guest
Posts: n/a
 
      12-14-2007
On Dec 14, 11:05 am, user923005 <dcor...@connx.com> wrote:
> On Dec 13, 9:56 pm, somenath <somenath...@gmail.com> 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
 
Reply With Quote
 
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>
Try the download section.


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

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      12-14-2007
somenath wrote:
> On Dec 14, 11:05 am, user923005 <dcor...@connx.com> wrote:
>> On Dec 13, 9:56 pm, somenath <somenath...@gmail.com> 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.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      12-14-2007
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) <kst->
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"
 
Reply With Quote
 
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/

 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      12-14-2007
Ian Collins <ian-> wrote:

> somenath wrote:
> > On Dec 14, 11:05 am, user923005 <dcor...@connx.com> wrote:
> >> On Dec 13, 9:56 pm, somenath <somenath...@gmail.com> 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
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      12-14-2007
Richard Bos wrote:
> Ian Collins <ian-> wrote:
>
>> somenath wrote:
>>> On Dec 14, 11:05 am, user923005 <dcor...@connx.com> wrote:
>>>> On Dec 13, 9:56 pm, somenath <somenath...@gmail.com> 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.
 
Reply With Quote
 
karancuteboy277@gmail.com
Guest
Posts: n/a
 
      12-14-2007
On Dec 14, 10:56 am, somenath <somenath...@gmail.com> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
short circuit evaluations JS# Java 5 11-21-2005 11:35 AM
help!,my flesh computer about to short circuit Steve Computer Support 3 08-06-2004 05:32 AM
New Releases: Short Circuit, Kung Fu Series, Image Horror: Updated complete downloadbable R1 DVD DB & info lists Doug MacLean DVD Video 0 12-13-2003 05:31 AM
short-circuit operators - part of the spec? Phil... Java 10 10-23-2003 08:42 PM
short-circuit operators - part of the spec? Phil... Java 1 10-22-2003 04:43 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57