当前位置:网站首页>C语言 -- 操作符详解
C语言 -- 操作符详解
2022-08-04 05:18:00 【稚子】
本节重点:
- 各种操作符的介绍
- 表达式求值
操作符分类:
- 算术操作符 + - * / %
/ 两边若都为整数,则得到的也为整数;两边有一个为小数,得到的即为小数。
% 操作符的两个操作数必须为整数,返回的是整除之后的余数。
- 移位操作符
#include <stdio.h>
#include <string.h>
// >> 右移操作符
// << 左移操作符
// 移动的为二进制位
int main()
{
int a = 16;
// 16的二进制0001 0000
int b = a >> 1;
printf("%d\n", a);
printf("%d\n", b);
return 0;
}
/*
右移操作符:
1.算术右移
右边丢弃,左边补原符号位;
2.逻辑右移
右边丢弃,左边补0
*/
整数的二进制表示有 :原码、反码、补码,存储到内存的是补码
- 位操作符
& 按位与
| 按位或
^ 按位异或
注:他们的操作数必须是整数
练习:交换两个变量的值,不使用中间变量
#include <stdio.h>
#include <string.h>
int main()
{
int a = 3;
int b = 5;
// 加减法
a = a + b;
b = a - b;
a = a - b;
printf("after : a = %d b = %d\n", a, b);
return 0;
}
练习:求一个整数存储在内存中的二进制中1的个数
#include <stdio.h>
int main()
{
int num = 0;
scanf("%d",&num);
// 统计num的补码中有几个1
int count = 0; // 计数
while (num)
{
if (num % 2 == 1)
count++;
num = num / 2;
}
printf("二进制中1的个数 = %d\n", count);
return 0;
}
- 赋值操作符
变量在创建时给它一个值,叫做初始化,之后叫赋值
- 单目操作符
单目操作符,只有一个操作数,例如!
双目操作符,两个操作数
&a 取地址操作符,一般与指针结合使用
*p 解引用操作符
*p = &a
// 变量的赋值与使用
int a = 10;
int *p_a = &a;
*p_a = 20;
sizeof计算的变量是所占内存空间的大小,单位是字节
int 4个字节
char 1个字节
#include <stdio.h>
int main()
{
short s = 0;
int a = 10;
printf("%d\n",sizeof(s = a+5)); // s为short,2个字节
printf("%d\n",s); // sizeof 不参与运算,只计算空间的大小
return 0;
}
++a 前置++,先++,后使用
a++ 后置++,先使用,再++
强制类型转换
int a = (int)3.14 //正确用法
int a = int(3.14) //错误用法
- 关系操作符
- 逻辑操作符
&& 注:只要左边结果为假,右边不管结果是什么都不再计算
int i = 0,a=0,b=2,c=3,d=4;
i = a++ && ++b && d++
printf("a = %d\n b= %d\n c=%d\n d = %d\n",a,b,c,d);
// 输出结果 1 2 3 4
|| 只要左边为真,右边就不需要计算了
- 条件操作符
- 逗号操作符
逗号表达式就是用逗号隔开的多个表达式,从左向右依次执行,整个表达式的结果是最后一个表达式的结果。
- 下标引用、函数调用和结构成员
下标引用操作符:一个数组名+一个索引值
函数调用操作符(),接受一个或多个操作数;第一个操作数是函数名,剩余的操作数就是传递给函数的参数。
访问一个结构成员(重要)
结构体变量.成员名
#include <stdio.h>
// 学生
// 创建一个结构体类型
struct stu{
char name[20];
int age;
char id[20];
};
// 结构体对象s1,并初始化,实例化
struct stu s1 = {"张三",20,"20131532"};
int main()
{
printf("%s\n",s1.name);
printf("%d\n",s1.age);
printf("%s\n",s1.id);
return 0;
}
结构体指针->成员名
#include <stdio.h>
// 学生
// 创建一个结构体类型
struct stu{
char name[20];
int age;
char id[20];
};
// 结构体对象s1,并初始化,实例化
struct stu s1 = {"张三",20,"20131532"};
struct stu *ps = &s1; // 结构体指针
int main()
{
printf("%s\n",ps->name);
printf("%d\n",ps->age);
printf("%s\n",ps->id);
//以下效果相同
printf("%s\n",(*ps).name);
printf("%d\n",(*ps).age);
printf("%s\n",(*ps).id);
return 0;
}
表达式求值
表达式求值的顺序一部分是由操作符的优先级和结合性决定,同样有些表达式的操作数在求值的过程中可能需要转换为其他类型。
隐式类型转换
C的整型算术运算总是至少以缺省整型类型的精度来进行的。为了获得这个精度,表达式中的字符和短整型操作数在使用之前被转换为普通整型,这种转换称为整型提升
整型提升是按照变量的数据类型的符号位来提升的
边栏推荐
猜你喜欢
el-Select selector bottom fixed
The difference between px, em, and rem
Converts XML tags to TXT format (voc conversion for yolo convenient training)
el-Select 选择器 底部固定
Turn: Management is the love of possibility, and managers must have the courage to break into the unknown
《看见新力量》第四期免费下载!走进十五位科技创业者的精彩故事
【云原生--Kubernetes】Pod资源管理与探针检测
[Evaluation model] Topsis method (pros and cons distance method)
3000字,一文带你搞懂机器学习!
解决错误:npm WARN config global `--global`, `--local` are deprecated
随机推荐
力扣:343. 整数拆分
字节最爱问的智力题,你会几道?
How to dynamically add script dependent scripts
动态规划总括
C专家编程 第5章 对链接的思考 5.4 警惕Interpositioning
[21 Days Learning Challenge] Image rotation problem (two-dimensional array)
力扣:746. 使用最小花费爬楼梯
数的划分之动态规划
败给“MySQL”的第60天,我重振旗鼓,四面拿下蚂蚁金服offer
[Cloud Native--Kubernetes] Pod Resource Management and Probe Detection
The symbol table
3面头条,花7天整理了面试题和学习笔记,已正式入职半个月
[Skill] Using Sentinel to achieve priority processing of requests
力扣:62.不同路径
路网编辑器技术预研
Interesting Kotlin 0x0E: DeepRecursiveFunction
The idea setting recognizes the .sql file type and other file types
心余力绌:企业面临的软件供应链安全困境
C Expert Programming Chapter 4 The Shocking Fact: Arrays and pointers are not the same 4.1 Arrays are not pointers
share总结