473,473 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Question regarding the short circuit behavior

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
Dec 14 '07 #1
17 1908
On Dec 13, 9:56 pm, somenath <somenath...@gmail.comwrote:
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
Dec 14 '07 #2
On Dec 14, 11:05 am, user923005 <dcor...@connx.comwrote:
On Dec 13, 9:56 pm, somenath <somenath...@gmail.comwrote:


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
Dec 14 '07 #3
somenath wrote:
On Dec 14, 11:05 am, user923005 <dcor...@connx.comwrote:
>On Dec 13, 9:56 pm, somenath <somenath...@gmail.comwrote:
>>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.
Dec 14 '07 #4
somenath <so*********@gmail.comwrites:
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) <ks***@mib.org>
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"
Dec 14 '07 #5
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/

Dec 14 '07 #6
Ian Collins <ia******@hotmail.comwrote:
somenath wrote:
On Dec 14, 11:05 am, user923005 <dcor...@connx.comwrote:
On Dec 13, 9:56 pm, somenath <somenath...@gmail.comwrote:
> 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
Dec 14 '07 #7
Richard Bos wrote:
Ian Collins <ia******@hotmail.comwrote:
>somenath wrote:
>>On Dec 14, 11:05 am, user923005 <dcor...@connx.comwrote:
On Dec 13, 9:56 pm, somenath <somenath...@gmail.comwrote:
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.
Dec 14 '07 #8
On Dec 14, 10:56 am, somenath <somenath...@gmail.comwrote:
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 '07 #9
In article <ec**********************************@e6g2000prf.g ooglegroups.com>,
somenath <so*********@gmail.comwrote:
>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
Dec 14 '07 #10
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
Dec 14 '07 #11
>>>>"CD" == Chris Dollin <eh@electrichedgehog.netwrites:

CDsomenath wrote:
>Hi All, I have one question regarding the bellow mentioned code
CDBELOW. 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
cw*****@chromatico.net
Dec 14 '07 #12
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

Dec 14 '07 #13
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
Dec 15 '07 #14
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.
Dec 15 '07 #15
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
Dec 15 '07 #16
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
Dec 16 '07 #17
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
Dec 16 '07 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Dave Opstad | last post by:
In this snippet: d = {'x': 1} value = d.get('x', bigscaryfunction()) the bigscaryfunction is always called, even though 'x' is a valid key. Is there a "short-circuit" version of get that...
8
by: der | last post by:
Hello all, I've a question about order of evaluations in expressions that have && and || operators in them. The question is: will the evalution go left-to-right, no matter what -- even if the...
26
by: myName | last post by:
a && b || c is evaluated as (a && b) || c or it is evaluated as a && (b || c)
2
by: webposter | last post by:
Hi, I am looking for information on a data structure (and associated algorithm) to do short-circuit evaluation of boolean expressions and haven't found a single one even after googing for two...
5
by: Phil Jones | last post by:
I'm just (as a matter of course) using AndAlso and OrElse statements to short circuit checking, even for nominal things like Boolean comparisons. I'm wondering, is that a good thing to do (for...
17
by: Dinsdale | last post by:
I would like to compare a string value to a pre-determined list of other strings. Is there a simple way to do this in one statements like this: if(strMystring.ToUpper() == ("STRING1"| "STRING2"|...
6
by: James Curran | last post by:
In recent weeks, I've twice come upon people who consider the short- curcuited evaluation of logical && (and ||) to be just a vendor- defined compiler quirk, and recommend against depending on it....
22
by: Cesar G. Miguel | last post by:
I've been studying python for 2 weeks now and got stucked in the following problem: for j in range(10): print j if(True): j=j+2 print 'interno',j What happens is that "j=j+2" inside IF...
39
by: Boltar | last post by:
Why does C do lazy evaluation for logical boolean operations but not bitwise ones? Ie: the following program prints "1 2" , not "1 1" under gcc main() { int a = 1; int b = 1; 0 && ++a;
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.