考研C语言第三章

3.1

运算

#include <stdio.h>

int main() {
    int result=4+5*2-6/3+10%4;
    printf("%d\n",result);
    return 0;
}

C语言输出上每次都要带着数据类型,想念java
算术运算符与关系运算符

#include <stdio.h>

int main() {
    int a;
    while(scanf("%d",&a))
    {
        if(3<a<10)
        {
            printf("between\n");
        }
        else{
            printf("not between\n");
        }
    }
    return 0;
}

这个输出结果就是:不管输入什么数字,输出的都是在中间,
原因:3<a<10是一个从左到右的计算,计算机并不认可
应该写

#include <stdio.h>

int main() {
    int a;
    while(scanf("%d",&a))
    {
        if(3<a&&a<10)
        {
            printf("between\n");
        }
        else{
            printf("not between\n");
        }
    }
    return 0;
}

要是用&&交,||并
逻辑运算符与逻辑表达式
判断是不是闰年小代码:

#include <stdio.h>
#include <stdlib.h>

int main() {
    int year;
    while(scanf("%d",&year))
    {
        if(year%4==0&&year%100!=0||year%400==0)
        {
            printf("runnian\n");
        }
        else{
            printf("not runnian\n");
        }
    }
}

逻辑非运算
非黑即白,非0即1

#include <stdio.h>
#include <stdlib.h>

int main() {
    int i=0,j=6;
    i=!!j;
    printf("i value =%d\n",i);
    printf("%d\n",!j);
    printf("%d\n",!!j);
    return 0;
}

运行结果:
在这里插入图片描述
短路运算(逻辑与和逻辑或)

#include <stdio.h>
#include <stdlib.h>
//短路运算
int main() {
    int i=0;
    i&&printf("can't see me");
    return 0;
}

当与运算左右两边出现0(很明显最后结果就是0,另一句不用算)
同理逻辑或的

#include <stdio.h>
#include <stdlib.h>
//短路运算
int main() {
    int i=1;
    i||printf("can't see me");
    return 0;
}

已经出现1了,另一边也不用算了
赋值运算符

#include <stdio.h>
#include <stdlib.h>

int main() {
    int a=1,b=2;
    //a=a+3;
    a+=3;//下面这个效率更高
    b*=5;
    printf("%d\n",a);//4
    printf("%d\n",b);//10
    return 0;
}


字节运算符sizeof
sizeof不是函数!不是函数!不是函数!

#include <stdio.h>
#include <stdlib.h>
//sizeof运算符
int main() {
    int i=0;
    printf("i size is %d\n",sizeof(i));
    return 0;
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值