C Programming (Operator's precedence)

sonxEr77

Expert Member
Joined
Oct 22, 2006
Messages
1,809
Reaction score
0
Location
around
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 ?
 
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
 
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!
 
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().
 
|| 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
 
Top
Sign up to the MyBroadband newsletter
X