View Full Version : C Programming (Operator's precedence)
sonxEr77
07-06-2007, 01:58 PM
in the following code
int i = -3,j= 2,k=0,m;
m = ++i || ++j && ++k;
printf("\n%d %d %d %d\n",i,j,k,m); [Output is -2 2 0 1]
My confusion is with k being displayed as 0 not 1....
is the epxression evaluated as
1. m = ++i || ( ++j && ++k;) 1st
or
2. m = (++i || ++j ) && ++k; 1st ?
Sharkey
07-06-2007, 04:02 PM
in the following code
int i = -3,j= 2,k=0,m;
m = ++i || ++j && ++k;
printf("\n%d %d %d %d\n",i,j,k,m); [Output is -2 2 0 1]
My confusion is with k being displayed as 0 not 1....
is the epxression evaluated as
1. m = ++i || ( ++j && ++k;) 1st
or
2. m = (++i || ++j ) && ++k; 1st ?
Hi. I dont see the point of your expression but I will explain what is happening. Unfortunately C++ order of precedence means that statement 1 is executed and if you look at it, it first evaluates ++i. As this is not zero, it returns true and because this result is || to the next piece, it never even executes the second part as an || expression requires either or both its operands to be non-zero. Thus neither j nor k's values are changed.
You should of course use brackets but if you want the same value for m and want the 2nd part executed, you could try to use the singular form of the || and && operators such as | and & this IMO would return [-2 3 1 1]
Hope this helps
Angstrom
07-06-2007, 04:18 PM
This feature is known as short-circuit boolean evaluation.
Personally, I love the way it works.
sonxEr77
07-06-2007, 06:51 PM
Hi. I dont see the point of your expression
A quiz related problem...
Unfortunately C++ order of precedence means that statement 1 is executed and if you look at it, it first evaluates ++i. As this is not zero, it returns true and because this result is || to the next piece, it never even executes the second part as an || expression requires either or both its operands to be non-zero. Thus neither j nor k's values are changed.
You should of course use brackets but if you want the same value for m and want the 2nd part executed, you could try to use the singular form of the || and && operators such as | and & this IMO would return [-2 3 1 1]
Hope this helps
Well this is the idea i got after execution.. My question is, what happens to the rule which says if operators are of same level in an expression, apply associativity (which should be R-L)? Or does '||' & '&&' overrides this rule?
Thanx BTW!
Angstrom
07-06-2007, 09:06 PM
My question is, what happens to the rule which says if operators are of same level in an expression, apply associativity (which should be R-L)? Or does '||' & '&&' overrides this rule?
Thanx BTW!
|| and && are left-to-right associative.
This is actually a really useful feature. For example:
if (foo() || bar())
If foo() returns anything other than 0 then there is no need whatsoever to execute bar(), so it's more efficient for one thing. Another example:
if (foo() && bar())
Similarly if foo() returns 0 then there is no need to waste time evaluating bar().
sonxEr77
07-06-2007, 09:38 PM
|| and && are left-to-right associative.
This is actually a really useful feature. For example:
if (foo() || bar())
If foo() returns anything other than 0 then there is no need whatsoever to execute bar(), so it's more efficient for one thing. Another example:
if (foo() && bar())
Similarly if foo() returns 0 then there is no need to waste time evaluating bar().
Alright then, make sense...thanx