If I have a bit of code looking like this:
if(someInteger || somecomplexfunction() > 0) {
// do something
}
Will the function be called if someInteger evaluates to true?
p.s. compiling with GCC with -O2
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No, it won’t. Logical operators in C short circuit, so if the left hand side of an
||is true, the right hand side won’t evaluate (and thus the function won’t execute, and no side effects that it might have will take effect). Likewise with&&, if the left hand side evaluates false, the right hand side won’t get evaluated.This is defined in the C standard and happens in any standards-compliant compiler regardless of compilation options.
While this sometimes leads to better performance, it’s not an optimization that compilers choose to make, it’s something that’s ingrained into the semantics of C.