Home >Backend Development >C++ >Short Circuit Evaluation in C : Is It as Reliable as in Java?
Short Circuit Evaluation in C : Is It as Guaranteed as in Java?
In Java, the short-circuit evaluation mechanism ensures that expressions are evaluated sequentially from left to right, stopping as soon as a false value is encountered. This behavior enables efficient use of conditions, such as:
if (a != null && a.fun());
Can C Offer the Same Guarantee?
In C , short-circuit evaluation is also employed for built-in data types and operators. However, the guarantee differs from Java.
if (a != 0 && a->fun());
Here, a != 0 evaluates to either true or false, and only if it evaluates to true, is a->fun() executed. This guaranteed behavior applies only to built-in types.
Overloading & and || in C
Custom types in C can overload the && and || operators. When this occurs, the short-circuited evaluation is not guaranteed. As a result, overloading these operators for custom types is generally discouraged.
The above is the detailed content of Short Circuit Evaluation in C : Is It as Reliable as in Java?. For more information, please follow other related articles on the PHP Chinese website!