当前位置:网站首页>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的整型算术运算总是至少以缺省整型类型的精度来进行的.为了获得这个精度,表达式中的字符和短整型操作数在使用之前被转换为普通整型,这种转换称为整型提升
整型提升是按照变量的数据类型的符号位来提升的
边栏推荐
- OpenRefine开源数据清洗软件的GREL语言
- MySQL数据库(基础)
- Resolved error: npm WARN config global `--global`, `--local` are deprecated
- 4.3 Annotation-based declarative transactions and XML-based declarative transactions
- Shocked, 99.9% of the students didn't really understand the immutability of strings
- Chapter 5 C programming expert thinking 5.4 alert Interpositioning of links
- 你以为border-radius只是圆角吗?【各种角度】
- Unity DOTS学习教程汇总
- 4.1 声明式事务之JdbcTemplate
- MySQL数据库面试题总结(2022最新版)
猜你喜欢
FPGA学习笔记——知识点总结
在被面试官说了无数次后,终于潜下心来整理了一下JVM的类加载器
el-Select selector bottom fixed
MySQL log articles, binlog log of MySQL log, detailed explanation of binlog log
读者让我总结一波 redis 面试题,现在肝出来了
leetcode 12. Integer to Roman numeral
数的划分之动态规划
Do you think border-radius is just rounded corners?【Various angles】
word 公式编辑器 键入技巧 | 写数学作业必备速查表
TSF微服务治理实战系列(一)——治理蓝图
随机推荐
如何低成本修bug?测试左移给你答案
Grain Mall - Basics (Project Introduction & Project Construction)
力扣:62.不同路径
[Evaluation model] Topsis method (pros and cons distance method)
解决安装nbextensions后使用Jupyter Notebook时出现template_paths相关错误的问题
npm安装依赖报错npm ERR! code ENOTFOUNDnpm ERR! syscall getaddrinfonpm ERR! errno ENOTFOUND
4.3 Annotation-based declarative transactions and XML-based declarative transactions
C Expert Programming Chapter 5 Thinking about Chaining 5.6 Take it easy --- see who's talking: take the Turning quiz
day13--postman interface test
BFC、IFC、GFC、FFC概念理解、布局规则、形成方法、用处浅析
9. Dynamic SQL
What is the salary of a software testing student?
自动化测试的成本高效果差,那么自动化测试的意义在哪呢?
5个开源组件管理小技巧
4.2 Declarative Transaction Concept
FPGA学习笔记——知识点总结
MySQL log articles, binlog log of MySQL log, detailed explanation of binlog log
7.16 Day22---MYSQL(Dao模式封装JDBC)
7、特殊SQL的执行
DP4398:国产兼容替代CS4398立体声24位/192kHz音频解码芯片