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