If I have an if statement in C that looks like:
if( function1() > 0 && function2() > 0 ){
//blah
}
Which function will execute first and will it always execute in that order?
Here function1()
is guaranteed to execute first.
The &&
operator is a short-circuiting operator. function2()
won't even be called unless the result of function1()
is greater than zero.
From the C99 standard:
Unlike the bitwise binary & operator, the && operator guarantees left-to-right evaluation; there is a sequence point after the evaluation of the first operand. If the first operand compares equal to 0, the second operand is not evaluated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With