Home > Backend Development > C++ > body text

Short Circuit Evaluation in C : Is It as Reliable as in Java?

Susan Sarandon
Release: 2024-11-10 04:41:02
Original
180 people have browsed it

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());
Copy after login

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());
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template