vaysagekv wrote:
) On 04/07/10 9:11 PM, Willem wrote:
)> vaysagekv wrote:
)> ) hi,
)> ) For the below program i expected output of -3 3 1 1 because precedance
)> ) of&& is greater than ||. But the actual output is -2 2 0 1. why?
)> )
)> ) int main ()
)> ) {
)> ) int i=-3,j=2,k=0,m;
)> ) m = ++i || ++j&& ++k;
)> ) printf("\n%d%d%d%d\n",i,j,k,m);
)> ) }
)>
)> Probably because you misunderstand how precedence works.
)> Perhaps you should explain in more detail why you expected that output.
)>
) I thought first && part will be calculated and then || part (i.e if we
) have expression like a+b*c,first b*c will be calculated right)
Well, yes and no. It's not specified in which order a, b and c are read.
Suppose you do a()+b()*c(), then it's entirely possible that a() will be
called first, and then b() and c().
Also, if you do a()+b() then it's also very possible that b() will be
called first, and then a().
But || and && are special. They evaluate the left side first, and
*only* if that's inconclusive, it will evaluate the right side.
So if you do a() || b(), then you *know* that a() will be called first.
SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT