What are the logical operators in C language?
C language provides a set of logical operators: or (||), and (&& ) and NOT (!) respectively correspond to the OR, AND, and NOT operations in propositional logic.
Logical operators: or ||. In propositional logic, when P=1 or Q=1, P||Q is equal to 1.
Logical operators: and &&. In propositional logic, when P=1 and Q=1, P&&Q is equal to 1.
Logical operator: not !. In propositional logic, when P equals 0, !P equals 1, and vice versa.
Recommended learning: c language video tutorial
Logical operations consider that all non-zero parameters are expressed as TRUE. While parameter 0 means FALSE, they return 1 (TRUE) or 0 (FALSE).
An important difference between the logical operators && and || and the corresponding bit-level operations & and | is that if the result of the expression can be determined by evaluating the first argument, then the logical operator The second argument will not be evaluated.
For x && y: When the logical AND left x is FALSE, the judgment on the right y is no longer performed, and the result is FALSE. Therefore, if a=0, the expression a && 5/a will not cause division by zero. For x || y: When the logical OR left x is FALSE, continue the judgment of the right y. If it is TRUE, the logical OR result is TRUE; when the logical OR left x is TRUE, the right y will no longer be judged, that is, logical or the result is TRUE.
The precedence of logical operators and other operators from low to high is:
Logical NOT! > Arithmetic operators> Relational operators> Logical AND &&, Logical or || > Assignment =
For more C language tutorials, please pay attention to PHP Chinese website!
The above is the detailed content of What are the logical operators in C language?. For more information, please follow other related articles on the PHP Chinese website!