当前位置:网站首页>C language -- operator details
C language -- operator details
2022-08-04 05:35:00 【 Childhood】
本节重点:
- 各种操作符的介绍
- 表达式求值
操作符分类:
- 算术操作符 + - * / %
/ If both sides are integers,The result is also an integer;There is a decimal on both sides,The result is a decimal.
% 操作符的两个操作数必须为整数,返回的是整除之后的余数.
- 移位操作符
#include <stdio.h>
#include <string.h>
// >> 右移操作符
// << 左移操作符
// The bits are moved
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 variable is given a value when it is created,叫做初始化,assignment
- 单目操作符
单目操作符,只有一个操作数,例如!
双目操作符,两个操作数
&a 取地址操作符,Typically used in conjunction with pointers
*p 解引用操作符
*p = &a
// Assignment and use of variables
int a = 10;
int *p_a = &a;
*p_a = 20;
sizeofThe calculated variable is the size of the memory space occupied,单位是字节
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 不参与运算,Only the size of the space is calculated
return 0;
}
++a 前置++,先++,后使用
a++ 后置++,先使用,再++
强制类型转换
int a = (int)3.14 //正确用法
int a = int(3.14) //错误用法
- 关系操作符
- 逻辑操作符
&& 注:As long as the left result is false,The right side is no longer calculated regardless of the result
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
|| 只要左边为真,The right side does not need to be calculated
- 条件操作符
- 逗号操作符
逗号表达式就是用逗号隔开的多个表达式,从左向右依次执行,整个表达式的结果是最后一个表达式的结果.
- 下标引用、函数调用和结构成员
下标引用操作符:一个数组名+一个索引值
函数调用操作符(),接受一个或多个操作数;第一个操作数是函数名,剩余的操作数就是传递给函数的参数.
访问一个结构成员(重要)
结构体变量.成员名
#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;
}
表达式求值
表达式求值的顺序一部分是由操作符的优先级和结合性决定,Also the operands of some expressions may need to be converted to other types during evaluation.
隐式类型转换
C的整型算术运算总是至少以缺省整型类型的精度来进行的.为了获得这个精度,表达式中的字符和短整型操作数在使用之前被转换为普通整型,这种转换称为整型提升
整型提升是按照变量的数据类型的符号位来提升的
边栏推荐
- 想好了吗?
- static在不同位置定义变量居然还有不同的含义?
- How to view sql execution plan offline collection
- Resolved error: npm WARN config global `--global`, `--local` are deprecated
- 3面头条,花7天整理了面试题和学习笔记,已正式入职半个月
- 力扣题解8/3
- 少年成就黑客,需要这些技能
- C Expert Programming Chapter 5 Thinking about Linking 5.2 Advantages of Dynamic Linking
- 7.13 Day20----MYSQL
- Grain Mall - Basics (Project Introduction & Project Construction)
猜你喜欢
Grain Mall - Basics (Project Introduction & Project Construction)
npm报错Beginning October 4, 2021, all connections to the npm registry - including for package installa
[Cloud Native--Kubernetes] Pod Resource Management and Probe Detection
SLSA 框架与软件供应链安全防护
Delphi-C端有趣的菜单操作界面设计
The symbol table
CentOS7 - yum install mysql
C语言 -- 操作符详解
Cannot read properties of null (reading ‘insertBefore‘)
读者让我总结一波 redis 面试题,现在肝出来了
随机推荐
嵌入式系统驱动初级【4】——字符设备驱动基础下_并发控制
Uni-app 小程序 App 的广告变现之路:全屏视频广告
TensorRTx-YOLOv5工程解读(一)
About yolo7 and gpu
The difference between px, em, and rem
想低成本保障软件安全?5大安全任务值得考虑
如何将 DevSecOps 引入企业?
string类简介
Unity自动生成阻挡Collider的GameObject工具
C Expert Programming Chapter 4 The Shocking Fact: Arrays and pointers are not the same 4.2 Why does my code not work
Interesting Kotlin 0x0E: DeepRecursiveFunction
力扣:70. 爬楼梯
8、自定义映射resultMap
C Expert Programming Chapter 5 Thinking about Linking 5.3 5 Special Secrets of Library Linking
注意!软件供应链安全挑战持续升级
Shocked, 99.9% of the students didn't really understand the immutability of strings
What is the salary of a software testing student?
The 2022 PMP exam has been delayed, should we be happy or worried?
leetcode 12. Integer to Roman numeral
【问题解决】同一机器上Flask部署TensorRT报错记录