C语言逻辑类型与运算

1,布尔类型

#include<stdbool.h>

可以使用ture,false.但不会输出。

#include <stdio.h>
#include <stdbool.h>
int main(){
    bool i=1;
    printf("%d",i);
    return 0;
}


//输出1

2,逻辑运算

逻辑非
&&逻辑与
||逻辑或

age为一个整数

!age<20  ——>  (!age)<20

//无论怎么样,都是真,因为!age 只能为0或1.

//    !是单目运算符,比双目的等级高

 3,优先级

 4,短路

若左边可以决定结果,就不会做右边的计算

3==6 && b+=1若左边为假,右边就不做了
3==6 || b+=1若左边为真,右边就不做了

以下代码的输出是 __0___.

int x=0, y=0, z=0;

z = (x==1) && (y=2);

printf("%d ", y);

(x==1)为假,&&左边为0,短路

#include <stdio.h>
int main(){
    int x,y;
    x=-1;
    if (x>0 && x++>=0){
    	y=100;
	}
	printf("x=%d y=%d",x,y);
    return 0;
}

//输出
x=-1 y=1

5,条件运算

比赋值运算符高,比其他低

count=(count>20)?count-10:count+10;

等价代码

#include <stdio.h>
int main(){
    int count=30;
    if (count>20){
    	count=count-10;
	}else{
		count=count+10;
	}
    return 0;
}

 6,逗号运算

最最最最低优先级,比赋值还低

运算结果为逗号后面的结果

#include <stdio.h>
int main(){
    int count;
	count=3+4,5+6;
	printf("%d",count);
    return 0;
}

//输出为7



int main(){
    int count;
	count=(3+4,5+6);
	printf("%d",count);
    return 0;
}

//输出为11

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值