当前位置:网站首页>Explain C language 10 in detail (C language series)
Explain C language 10 in detail (C language series)
2022-06-26 13:03:00 【Cream dimple* ٩ ( ˊωˋ*)و*】
Catalog
Subscript reference operator :
Structure member access operator :
Preface :
In the beginning C The language editor shared some small knowledge about operators with you and learned about operators , Next, I'll take you to learn more about operators and appreciate different operators .
The operator :
Arithmetic operators :
Arithmetic operators have :/、-、+、*、%
1.% : among % Operands at both ends must be integer .
2. / : When calculating, if both ends are integers, only the integer part will be retained in the calculation result , If one of the two ends is a floating-point type, the result of the calculation is a floating-point type .( As shown in the following code )
Code :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
float a = 6 / 5;
float b = 6.0 / 5;
float c = 6 / 5.0;
printf("%f\n", a);
printf("%f\n", b);
printf("%f\n", c);
return 0;
}Code screenshot :

Shift operator :
Shift operator :<< 、>>
<<: Move left
characteristic : The one on the left is discarded , The right to repair 0.
Code :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int a = 2;
int b = a << 1;
printf("%d\n", a);// remain unchanged - 2
printf("%d\n", b);// take a Move the binary bit of one bit to the left - 4
return 0;
}Code screenshot :

Analysis diagram :
>>: Move right
There is a difference between moving right and moving left , There are two ways to shift right :
1. Count right : On the right side of the discarded , Left complement sign bit .
2. Logical shift right : On the right side of the discarded , Left complement 0.

Bit operators :
Bit operators have :&、|、^
Be careful : The operand of a bitwise operator must be an integer
&: Bitwise AND ( And by bits )
example :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int a = 3;
int b = 5;
int c = a & b;
printf("%d\n", c);// 1
return 0;
}The resolution is as follows :
|: Press bit or ( To operate on or by bits )
example :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int a = 3;
int b = 5;
int c = a | b;
printf("%d\n", c);// 7
return 0;
}The resolution is as follows :

^: Bitwise XOR
The rules : Same as 0, Different for 1.
example :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int a = 3;
int b = 5;
int c = a ^ b;
printf("%d\n", c);// 6
return 0;
}The resolution is as follows :

Assignment operator :
Assignment operators have :=、+=、*=、/=、>>=、<<=、%=
Be careful := Is to assign values ,== It's judgment .
Monocular operators :
Monocular operators have :!、-、+、&、sizeof、~、--、++、*、( type )
!: Logical inverse operator
The code is as follows :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int flage = 0;
if (flage)//flage Print when true haha
printf("haha\n");
if (!flage)//flage Print when false xixi
printf("xixi\n");
return 0;
} The code runs as follows :
sizeof: Calculates the type length of the operand ( In bytes )
The code is as follows :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
short a = 0;
int b = 0;
int arr1[10] = { 0 };
char arr2[10] = { 0 };
printf("%d\n", sizeof(a));
printf("%d\n", sizeof a);// Parentheses can be omitted when calculating as variable names
printf("%d\n", sizeof(int));// Do not omit parentheses when calculating as keywords
printf("%d\n", sizeof(b));
printf("%d\n", sizeof(arr1));// You can calculate the size of the array
printf("%d\n", sizeof(arr2));
return 0;
} The code screenshot is as follows :
~: The binary bit of a number is reversed bit by bit
&: Take the address operator
*: Dereference operator
The code is as follows :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int a = 4;
int* pa = &a;//*--- explain pa It's a pointer variable
*pa = 10;//*-- Yes pa Quoting
printf("%d\n", a);
return 0;
}The code screenshot is as follows :

( type ): Cast
The code is as follows :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int a = (int)3.14;
printf("%d\n", a);
return 0;
}
The code screenshot is as follows :

Relational operator :
Relational operators have :>、>=、<、<=、!=、==
Logical operators :
Logical operators have :&&、||
&&: Logic and
The code is as follows :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int a = 2;
int b = 0;
if (a && b)// False, false
{
printf("haha\n");
}
else
{
printf("xixi\n");
}
return 0;
}The code screenshot is as follows :

||: Logic or
The code is as follows :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int a = 2;
int b = 0;
if (a || b)// There are really true
{
printf("haha\n");
}
else
{
printf("xixi\n");
}
return 0;
} The code screenshot is as follows :
Be careful :
1.&&: If one is 0 Then the following expressions do not need to be calculated .
2.||: If one is 1 Then the following expressions do not need to be calculated .
example :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int i = 0, a = 0, b = 2, c = 3, d = 4;
//1.&&
i = a++ && ++b && d++;
printf("a = %d\nb = %d\nc = %d\nd = %d\n", a, b, c, d);
//2.||
//i = a++ || ++b || d++;
//printf("a = %d\nb = %d\nc = %d\nd = %d\n", a, b, c, d);
return 0;
} among 1.&& The running results of are as follows :
2.|| The running results of are as follows :
Conditional operators :
The conditional operator is also called the trinocular operator ,(exp1?exp2:exp3)
if exp1 If yes, the calculation result is exp2 Otherwise, the value of exp3 Value .
The code is as follows :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
// Use the conditional operator to calculate the maximum value
int main()
{
int a = 4;
int b = 7;
int max = (a > b ? a : b);
printf("max = %d\n", max);
return 0;
}The code screenshot is as follows :

Comma expression :
(exp1,exp2,exp3,……,expn)
The value of the comma expression is expn Value .
The code is as follows :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int a = 3;
int b = 4;
int c = 5;
int d = (a++, c = a + b, b = a + c);
printf("d = %d\n", d);// The result of the calculation is b Value
return 0;
} The code screenshot is as follows :
Subscript reference operator :
[ ]: Examples are as follows .
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int arr[5] = { 1,2,3,4,5 };
printf("%d\n", arr[3]);// among [] Reference operator for subscript
return 0;
} The screenshot of the code running is as follows :
Function call operator :
(): Examples are as follows .
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int Add(int x, int y)
{
return x + y;
}
int main()
{
int a = 2;
int b = 3;
int ret = Add(a, b);// among () Call the operator for the function
printf("ret = %d\n", ret);
return 0;
}
The screenshot of the code running is as follows :
Structure member access operator :
The structure member access operators are :.、->
The code is as follows :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
struct Book
{
char name[10];
char id[10];
int price;
};
int main()
{
struct Book b = {"C Language ","0x0060",30};
struct Book* pb = &b;
// adopt . Operator to access
printf("%s\n", b.name);
printf("%s\n", b.id);
printf("%d\n", b.price);
printf("%s\n", (*pb).name);
printf("%s\n", (*pb).id);
printf("%d\n", (*pb).price);
// adopt -> To visit
printf("%s\n", pb->name);
printf("%s\n", pb->id);
printf("%d\n", pb->price);
return 0;
}The code screenshot is as follows :

Expression evaluation :
1. Implicit type conversion :
Improve the overall shape : Integer promotion is promoted according to the sign bit of variable type .
How should I fill the previous position when I am promoted ? The rules are as follows .
Signed bit :
negative : repair 1.
Positive numbers : repair 0.
Unsigned bit : High direct compensation 0.
example :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
char a = 3;
char b = 127;
char c = a + b;
printf("%d\n", c);
return 0;
} Screenshot of code running :
The analysis is shown in the figure below :

2. Arithmetic conversion :
To byte long , High precision conversion .
3. Operator properties :
influence :
①: The priority of the operator .
②: The associativity of operators .
③: Whether to control the order of evaluation .
summary : Although we have the precedence of many operators , But the expression we write cannot determine the unique calculation path through the properties of the operator , So this expression is the problem expression .
Conclusion :
This time I share with you C The operators we often see in languages , I hope that's helpful , Students who want to learn remember to pay attention to Xiaobian and study together ! If there are any mistakes in the article, you are also welcome to point out the maze for Xiaobian in time ( I'd like to thank you guys first !)
边栏推荐
- 【网络是怎么连接的】第二章(上): 建立连接,传输数据,断开连接
- [BSidesCF 2019]Kookie 1
- 中国剩余定理模板题 互质与非互质
- Electron official docs series: Development
- Summary of some application research cases of UAV Remote Sensing in forest monitoring
- 小白懒人专用-win10-win11一键安装版
- Source code learning: atomicinteger class code internal logic
- ES6模块
- opencv高速下载
- This function has none of deterministic, no SQL solution
猜你喜欢

A must for programmers, an artifact utools that can improve your work efficiency n times

【网络是怎么连接的】第二章(中):一个网络包的发出

Software testing - concept
![[esp32-c3][rt-thread] run RT-Thread BSP minimum system based on esp32c3](/img/4a/503240b332e3279047c438f1d9845e.png)
[esp32-c3][rt-thread] run RT-Thread BSP minimum system based on esp32c3

国标GB28181协议EasyGBS视频平台TCP主动模式拉流异常情况修复

倍福PLC通过MC_ReadParameter读取NC轴的配置参数

倍福TwinCAT通过Emergency Scan快速检测物理连接和EtherCAT网络
![P5733 [deep foundation 6. example 1] automatic correction](/img/34/081dbd805593a92a86c3081d6772e3.png)
P5733 [deep foundation 6. example 1] automatic correction

快手实时数仓保障体系研发实践

倍福NC轴状态转移图解析
随机推荐
NoSQL mongodb - 04 mongodb database and web service combination case
软件测试 - 概念篇
map 取值
Typescript
Biff TwinCAT can quickly detect the physical connection and EtherCAT network through emergency scan
Stream流学习记录
倍福PLC旋切基本原理和应用例程
中国剩余定理模板题 互质与非互质
A must for programmers, an artifact utools that can improve your work efficiency n times
Echart堆叠柱状图:色块之间添加白色间距效果设置
postgis計算角度
System tasks (display / print class) in Verilog - $display, $write, $strobe, $monitor
Deep parsing MySQL binlog
.NET MAUI 性能提升
【网络是怎么连接的】第一章:浏览器生成消息
Tiger DAO VC产品正式上线,Seektiger生态的有力补充
别乱用 FULL_CASE 和 PARALLEL_CASE
轻流完成与「DaoCloud Enterprise 云原生应用云平台」兼容性认证
Adobe Acrobat阻止30款安全软件查看PDF文件 或存在安全风险
[esp32-c3][rt-thread] run RT-Thread BSP minimum system based on esp32c3