What is the precedence of logical operators in C language?

烟雨青岚
Release: 2020-07-02 09:27:05
Original
15292 people have browsed it

The precedence of logical operators in C language from high to low is:! (logical NOT), && (logical AND), || (logical OR). The value of a logical expression is a logical value; logical values ​​are divided into logical true values ​​and logical false values. During judgment, only zero values ​​are judged as logical false values ​​(false), and all non-zero values ​​can be judged as logical true values. value(true).

What is the precedence of logical operators in C language?

#C language provides the following three logical operators.

One yuan:! (logical not). Binary: && (logical AND), || (logical OR).

Among the above three logical operators, logical NOT ! has the highest priority, logical AND && comes second, and logical OR || has the lowest priority.

The value of a logical expression is a logical value, that is, Boolean type (bool). This type is new in C99. Some compilers may not support this type yet.

Logical values ​​are divided into logical true values ​​and logical false values. Under normal circumstances, during judgment, only zero values ​​are judged as logical false values ​​(false), and all non-zero values ​​can be judged as logical true values ​​(true); when storing and representing, usually, 1 is used to represent logical values. True value, 0 represents a logical false value.

The operation rules of logical AND && operator: The result is true only when both operands are logically true. In all other cases, the result is false.

The operation rules of logical OR || operator: The result is false only when both operands are logically false. In all other cases, the result is true.

int a=3,b=5;
Copy after login

Then there are:

!a: Since a is non-zero, it is true, !a is false, and its value is 0.

a||b: Since both a and b are non-zero and both are true, the result of logical OR is true and its value is 1.

a&&b: Since both a and b are non-zero and both are true, the result of logical AND is true and its value is 1.

!a||b&&2: Since logical negation! has the highest priority, it is combined with a first, and && has a higher priority than ||, which is equivalent to (!a)||(b&&2), that is, 0|| 1 is true, its value is 1.

Logical AND &&, logical OR || all have "short circuit" characteristics:

Logical AND && "short circuit": When the left operand of logical AND && is logical When false, it is enough to judge that the result of the logical operation is false, so the right operand will no longer be executed.

Logical OR || "Short circuit": When the left operand of logical OR || is logically true, it is enough to judge that the result of the logical operation is true, so the right operand will no longer be executed.

Recommended tutorial: "C Language"

The above is the detailed content of What is the precedence of logical operators in C language?. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template