f=++i||(k++)&&1; how are such expressions evaluated??

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • abhi17
    New Member
    • Feb 2010
    • 6

    f=++i||(k++)&&1; how are such expressions evaluated??

    f=++i||(k++)&&1 ;
    how are such expressions evaluated??
    in my code this k never got incremented (i!=-1)


    incremnt and decrement operators have higher precedence than && and || then shouldn't this mean that all these unary operators are evaluated in a logical expression
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    Code:
    f = ++i || (k++) && 1;
    g = A || B && C
    The second statement is a more general version of the first. Use of logical operators means that g is assigned the value 1 if the expression is true or 0 if it is false.

    The first logical operator is || (logical-or). This operator takes two arguments. The result is true if either argument is nonzero. If the first argument is nonzero then there is no need to evaluate the second argument. In fact, the C standard guarantees that it will not evaluate the second argument in this case. There is a similar guarantee for the && operator (logical-and) when its first argument is zero. If the second argument is not evaluated then side-effects associated with it do not occur. Refer to short-circuit evaluation for more details.

    Comment

    Working...