Is the order of boolean
expressions in this if
statement fixed?
if(boolean_expression_1 || boolean_expression_2) {
}
Is boolean_expression_1
always evaluated before boolean_expression_2
? Is the order of evaluation a standard in C
?
Is the order of boolean
expressions in this if
statement fixed?
if(boolean_expression_1 || boolean_expression_2) {
}
Is boolean_expression_1
always evaluated before boolean_expression_2
? Is the order of evaluation a standard in C
?
Yes. It's guaranteed. It's called "short circuit" evaluation.
From C11 draft, 6.5.14 Logical OR operator:
Unlike the bitwise | operator, the || operator guarantees left-to-right evaluation; if the second operand is evaluated, there is a sequence point between the evaluations of the first and second operands. If the first operand compares unequal to 0, the second operand is not evaluated.
(emphasis mine).