Consider the following code:
int y, x; y = 2; x = 0;
x!=0&&y/x>5
My lecturer's textbook on C provides a table for "Precedence hierarchy for the arithmetic, relational, and logical operators" using this table to evaluate the operators in the above expression I get the following precedence:
(1) is /, therefore, do y/x first
(2) is >, (3) is !=, (4) is &&.
If I evaluate the above code using this precedence hierarchy, then the first subexpr to be evaluated is: y / x which is 2 / 0, or undefined...
The given answer is 0.
Later on, the textbook also states that, per the ANSI C standard, the operand on the left of && will always be evaluated first, and only if this is 0 will the operand on the right be evaluated.
Does this mean that whenever a logical and (&&) (or a logical or (||) for that matter) appears in an expression along with arithmetic and relational operators, that effectively I need to separate the terms of the overall expression into two groups - terms on the left, and terms on the right, of the &&, and ONLY THEN begin applying the "Precedence hierarchy for the arithmetic, relational, and logical operators" to the operators contained in the overall 'left hand operand'?
This would result in the above expression being evaluated as:
(x!=0) && (y/x>5)
starting with the left operand, (x!=0), which is (0!=0), which is false, so 0, and thus no further evaluation takes place.
--
If this is the case, why does the && operator appear so low down the hierarchy of precedence if its inclusion in an expression dictates what must be done first?