6. 逻辑运算
6.1逻辑运算:&&、||、!:&&表示当两个表示都为真时,结果为真
||两个表达式一个为真既为真
!表达式为真取反为假,表达式为j假取反为真
6.2短路与、短路或,如果第一个表达式能确定最终结果,则后面的表达式将不再计算
#include <stdio.h>
// 逻辑运算
void test1() {
int a = 1;
int b = 2;
int c = 3;
int result;
result = a > b && a > c;
printf("result=%d\n", result);
result = a < b && a < c;
printf("result=%d\n", result);
printf("-------------\n");
result = a > b || c > b;
printf("result=%d\n", result);
printf("-------------\n");
result = !a;
printf("result=%d\n", result);
}
// 短路
void test2() {
int a = 1, b = 2, c = 3;
int result;
result = a > b && b++ > c;
printf("result=%d\n", result);
printf("b=%d\n", b);
printf("----------------\n");
result = a < b || b++ < c;
printf("result=%d\n", result);
printf("b=%d\n", b);
}
int main(void) {
// test1();
test2();
return 0;
}
该博客教程视频地址:http://geek99.com/node/975