Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C Programming
Short-circuiting in C
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="James Kuyper, post: 4215081"] I assume that, if you're willing to have the entire condition evaluated, then neither b nor c represent expressions with side-effects. In particular, neither of them is or involves the value of an object defined with volatile. Then there's two basic cases that need to be distinguished: 1. The compiler can figure out that neither b nor c are expressions with side effects. If this is the case, then any sufficiently good compiler targeting your platform should generate code that evaluates both b and c, and then discarding their values if not needed; at least, it should do so whenever it makes sense to do so. This is allowed by the C standard, even though it says explicitly that they should only be evaluated if needed. That's because of the fact that they have no side-effects. As a result, there's no way for a strictly conforming program to determine whether or not the compiler is doing this, so the as-if rule allows the compiler to do this. If your compiler isn't smart enough to perform this optimization automatically, there's no way in C to tell it explicitly to perform it. 2. The compiler cannot figure out that neither b nor c have side-effects. About the only way this could be true is if one or both of those expressions involve a function call. If all three expressions have integral type, try: if(a & b & c) { } The use of '&' rather than '&&' removes the short-circuiting behavior, but the net result is the same, as long as integer types are involved. If any of the expressions has a floating point or pointer type, you'll have to insert !=0, and that will probably be converted into a branch, which is precisely what you're trying to avoid. [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C Programming
Short-circuiting in C
Top