-
precedence problem
Code:
# include <stdio.h>
int main()
{
int i=-3,j=2,k=0,m;
m=++i || (++j && ++k) ;
printf("%d %d %d %d\n",i,j,k,m);
return 0 ;
}
Output: -2 2 0 1
why not expression (++j && ++k) is executing.Precedence of ( ) is higher than || (even though left hand side of OR is TRUE).
Please clear my doubt.
-
Recall the short circuit evaluation of the || and && operators.
Also, note that precedence is about grouping, not order of evaluation, even though they are sometimes related.
-
What laserlight said. Also, logical || and && operators introduce a sequence point: Sequence point - Wikipedia, the free encyclopedia. Due to this, the left side is always fully evaluated and all side effects settled out before the right side is touched.
-
And anyone that would use code like that in a production environment would be shot at dawn.