Question regarding the short circuit behavior

S

somenath

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
 
U

user923005

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?
 
S

somenath

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
 
C

CBFalconer

somenath said:
#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.
 
I

Ian Collins

somenath said:
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.
 
K

Keith Thompson

somenath said:
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.
 
C

Chris Dollin

somenath said:
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.
 
R

Richard Bos

Nope, that's too simplistic.
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
 
I

Ian Collins

Richard said:
Nope, that's too simplistic.


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!
 
K

karancuteboy277

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.
 
R

Richard Tobin

somenath said:
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
 
P

pete

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)
 
C

Charlton Wilbur

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
 
T

Thad Smith

pete said:
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 */
 
H

Harald van Dijk

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.
 
T

Thad Smith

Harald said:
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.
 
P

Peter Nilsson

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.
 
P

pete

Peter said:
Almost.

If x is an unsigned short

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


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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top