单目操作符的理解

!——逻辑反操作

#include <stdio.h>

int main()
{
    int a = 0;
    if(!a)
    {
        printf("zzz\n");
    }
    return 0;
}

+  -  正负值

#include <stdio.h>

int main()
{
    int a = 10;
    int b = +a;
    int c = -a;
    printf("%d %d %d\n",a,b,c);
    return 0;
}

&——取地址

#include <stdio.h>

int main()
{
    int a = 10;
    int* p = &a;//a变量的地址
    int arr[10];
    int (*pa)[10] = &arr;//数组的地址
    return 0;
}

*——解引用(间接访问操作符)

#include <stdio.h>

int main()
{
    int a = 10;
    int* p = &a;
    *p;//对p进行解引用操作,*p是通过p中存放的地址,找到p所指向的对象
    return 0;
}

注意:取地址和*是一来一回的,可以配合着使用

sizeof内存大小计算

sizeof是在计算类型创建变量或者变量的大小,单位是字节

sizeof计算的结果是size_t类型的

size_t 是无符号整型的

对size_t类型的数据进行打印的时候可以使用%zd

对于sizeof后面的括号,当括号中的内容不是类型名的时候,括号可以省略,这样也就说明sizeof不算函数

sizeof是操作符——单目操作符

#include <stdio.h>

int main()
{
    int a = 10;
    printf("%zd\n",sizeof(a));
    printf("%zd\n",sizeof a);
    printf("%u\n",sizeof a);
    printf("%zd\n",sizeof(int));

    int arr[10] = {0};
    printf("%zd\n",sizeof(arr));
    printf("%zd",sizeof(arr) / sizeof(arr[0]));//数组元素的个数
    return 0;
}

~——对一个数的二进制位按位取反

#include <stdio.h>

int main()
{
    int a = 0;//00000000000000000000000000000000
    printf("%d\n",~a);//10000000000000000000000000000001
    return 0;
}
#include <stdio.h>

int main()
{
    int a = 10;//00000000000000000000000000001010
    a |= (1 << 4);//00000000000000000000000000010000 1 << 4
    printf("%d\n",a);
    a &= ~(1 << 4);//将a还原成初始化时候的值
    printf("%d\n",a);
    return 0;
}

++——自增操作符

#include <stdio.h>

int main()
{
    int a = 1,c = 1;
    int b = ++a;//相当于a = a + 1,b = a;
    printf("%d\n",b);
    int d = c++;//相当于d = c,c = c + 1;
    printf("%d\n",d);
    return 0;
}

--——自减操作符

#include <stdio.h>

int main()
{
    int a = 5;
    int b = --a;//相当于 a = a - 1,b = a;
    printf("%d %d\n",a,b);
    int c = 9;
    int d = c--;//相当于d = c,c = c - 1
    printf("%d %d\n",c,d);
}

( )——强制类型转换

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

int main()
{
    int a = (int)3.14//3.14被编译器识别为double类型;
    srand((unsigned int)time(NULL));//time_t类型,强制转换成无符号整型
    rand();
    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值