In high school mathematics, we have learned logic operations, such as p for true proposition, Q on false proposition, then "P and Q" is false, "p or Q" For true, "non q" is true.
In the C language, there are similar logical operations:
operator |
Description |
combination of |
examples |
&& |
and operation, binocular, corresponding to the mathematical "and" |
Left combined |
1&&0, (9>3) && (b>a) |
|| |
Or operation, binocular |
Left combined |
1| | 0. (9>3) | | (B>a) |
! |
Not an operation. Monocular |
Right combination |
!a,! (2<5) |
The result of a logical operation
In programming, we typically refer to a 0 value as "false" and a non-0 value as "true." The result of the logical operation is only "true" and "false", "true" corresponding to the value of 1, "false" corresponding to the value of 0.
1) and operation (&&)
The result is true when the two quantities of the participating operations are true, otherwise it is false. For example:
5&&0
5 is True, 0 is false, and the result of phase is false, that is, 0.
(5>0) && (4>2)
The result of 5>0 is 1, true, 4>2 result is 1, also true, so the result of phase is true, that is 1.
2) or operation (| |)
Two of the number of participating operations as long as one is true, the result is true, and the two quantities are false, the result is false. For example:
10 | | 0
10 is true, 0 is false, the result of phase or is true, that is, 1.
(5>0) | | (5>8)
The result of 5>0 is 1, true, the result of 5>8 is 0, false, so the result of phase or is true, that is 1.
3) Non-operation (!)
The result is false when the amount of the participating operation is true, and the result is true when the amount of the participating operation is false. For example:
!0
0 is false, the result of the non operation is true, that is, 1.
! (5>0)
The result of the 5>0 is 1, true, and the result of the operation is false, that is, 0.
The result of the output logical operation:
#include <stdio.h>
int main () {
int a = 0, B = ten, c =-6;
int result_1 = a&&b, result_2 = c| | 0;
printf ("%d,%d\n", result_1,!c);
printf ("%d,%d\n", 9&&0, result_2);
printf ("%d,%d\n", b| | 0&&0);
return 0;
}
Run Result:
0, 0
0, 1
1, 0
Priority level
logical operators and other operator precedence from lowest to highest:
Assignment operators (=) < && and | | < relational operator < arithmetic operator < non (!)
&& and | | Below the relational operator,! is higher than the arithmetic operator.
According to the precedence order of the operators, you can draw:
A>b && c>d equivalent to (a>b) && (c>d)
!b==c| | D<a equivalent to ((!b) ==c) | | (D<a)
A+b>c&&x+y<b equivalent to ((a+b) >c) && ((x+y) <b)
In addition, logical expressions can also be nested, such as a>b && B | | 9>c,a | | C>d &&!p.
Logical operators for example:
#include <stdio.h>
int main () {
char c= ' k ';
int i=1,j=2,k=3;
float x=3e+5,y=0.85;
printf ("%d,%d\n",!x*!y,!!! x);
printf ("%d,%d\n", x| | I&&j-3, i<j&&x<y);
printf ("%d,%d\n", i==5&&c&& (j=8), x+y| | I+J+K);
return 0;
}
Run Result:
0,0
1,0
0,1
In this case, the!x and!y respectively are 0,!x*!y 0, so the output value is 0. Since x is not 0, it!!! The logical value of x is 0. to X| | I && j-3, first calculated j-3 value is not 0, and then I && j-3 logical value of 1, so x| | The logical value of the i&&j-3 is 1. For I<j&&x<y, because the value of I<J is 1, and X<y is 0, the value of the expression is 1, 0 phase, and finally 0, the i==5&&c&& (j=8) type, because I==5 is false, The value is 0, and the expression consists of two and operations, so the entire expression has a value of 0. For type x+ y| | I+j+k because the value of X+y is not 0, the value of the whole or expression is 1.
The above is the C language logical operator Knowledge collation, hope to help learn C language students