当前位置:网站首页>Embedded-c Language-2
Embedded-c Language-2
2022-07-05 17:04:00 【Orange peel does not stop school】
One 、 about C Operators and expressions of language
1. Operator
1.1 Arithmetic operator : Add , reduce , ride , except , Remainder , Corresponding symbol :+,-,*,/,%
<1>. If the two numbers involved in the division calculation are integer numbers, the calculation result is only Keep the integer part
for example :5/2=2
<2>./ and % Can't be used on integers 0 To operate , Otherwise, the program crashes
for example :5/0, 5%0
<3>.% Floating point numbers cannot be used
for example :5%1.1, Otherwise, the program crashes
<4>.% Result It is consistent with the number symbol on the left
for example :-7%2=-1 7%-2=1
<5>./ If The divisor is a floating point number , Finally get inf infinity
for example :5/0.0
1.2 Assignment operator :=, Is to give the value on the right to the variable on the left , That is to change the memory number corresponding to the variable
form 1:
int a;
a = 10
form 2:
int a, b, c;
a = b = c = 10;
printf("%d %d %\n", a, b, c); //10 10 10
form 3: Composite operators : Assignment operators are used in conjunction with other operators
for example :
a += b; // Equivalent to a = a + b;
a -= b; // Equivalent to a = a - b;
a *= b; // Equivalent to a = a * b;
a /= b; // Equivalent to a = a/b;
a %=b; // Equivalent to a = a%b;
Pay attention to the realization of : Cannot give constant ( Unchangeable numbers ) And expression assignment :
100 = 200; //gcc Report errors
100 = a; //gcc Report errors
a+b = c; //gcc Report errors
1.3 Self - increment operator (++) And the autodecrement operator (--)
<1>. Definition
The auto increment operator is to add the memory value corresponding to the variable 1
The self subtraction operator is to subtract the memory value corresponding to the variable 1
<2>. Four forms :
front ++: First, add... To the value of the variable 1, Calculate the value of the expression after
for example :
int a = 1;
int b = 0;
b = ++a;
printf("a = %d, b = %d\n", a, b); //a = 2, b = 2
after ++: First calculate the value of the expression , After that, add... To the value of the variable 1
for example :
int a = 1;
int b = 0;
b = a++;
printf("a = %d, b = %d\n", a, b); //a = 2, b = 1
front --: First, subtract the value of the variable 1, Calculate the value of the expression after
for example :
int a = 2;
int b = 0;
b = --a;
printf("a = %d, b = %d\n", a, b); //a = 1, b = 1
after --: First calculate the value of the expression , Then subtract the value of the variable 1
for example :
int a = 2;
int b = 0;
b = a--;
printf("a = %d, b = %d\n", a, b); //a = 1, b = 2
<3>. Do not add or subtract constants
100++; // Report errors
++100; // Report errors
1.4 Relational operator :
== be equal to /!= It's not equal to /> Greater than /< Less than />= Greater than or equal to /<= Less than or equal to
Be careful :
<1>. The result of a relational operator is an integer :1( really ) perhaps 0( false )
<2>. Do not make the following logical judgments ( Continuous logical judgment )
for example :8<7<5, The result is always 1( really )
Because first 8<7, The result is 0, and 0 There is less than 5, The result is 1
1.5 Logical operators
<1>. clear : What is true in a computer is false 0( Include 1),0 It's fake
Logical operator action : Realize logical judgment in computer program
for example : When the user name and password are correct , To log in
The login method can be wechat or mobile phone number, and the latter can be Tiktok number
<2>. Logical operator types : Three types of
1. Logic and : &&( also , At the same time )
2. Logic or : ||( Or it means )
3. Logic is not : !( The meaning of doing opposite )
<3>. Logic and && Operator characteristics
Use the syntax :C= expression A && expression B
semantics : Only when A and B It's all true ( Not 0), Entire expression C The value of is true
As long as one is false , Entire expression C All values are false
for example :1 && 1; // really
0 && 1; // false
1 && 0; // false
0 && 0; // false
<4>. Logic or || Operator characteristics
Use the syntax :C= expression A || expression B
semantics : as long as A and B One of them is true , Entire expression C It's all true
Only when A and B It's all fake , The result can be false
for example :0 || 0; // false
1 || 0; // really
0 || 1; // really
1 || 1; // really
<5>. Characteristics of logical non operators
Use the syntax :! expression A
semantics :A False result is true ,A True results are false
for example :
!1;// The result is false
!0;// The result is true
<6>. Bear in mind : Short-circuit operation ( The written examination questions are required )
form 1.A && B: If A The value of is false , be B Don't deal with ,B The code does not execute
for example :
int a = 1;
0 && ++a;
printf("a = %d\n", a); //a=1
int b = 1;
250 && ++b;
printf("b = %d\n", b); //b=2
form 2.A || B: If A The value of is true , be B Don't deal with ,B The code does not execute
for example :
int a = 1;
0 || ++a;
printf("a = %d\n", a); //a=2
int b = 1;
250 || ++b;
printf("b = %d\n", b); //b=1
1.6 position (bit) Operator
<1>. All the data in the computer ( Numbers ) All in memory , And they are stored in binary form (1 perhaps 0,bit position ) and 8,10,16 Base is just an artificial friendly representation of binary numbers ( To show )
<2>. Bitwise operators work : Bit operations are operations on binary numbers in memory , also C Language specifically provides corresponding operators , Abbreviated as bitwise operator
<3>. There are four forms of bitwise operators :
Bit and :&( clear 0)
Bit or : |( Set up 1)
Bit exclusive or : ^( reverse )
It's the opposite : ~( Take the opposite )
<4>. Bit and operator characteristic :
grammar :C = A & B;
for example :
A: 01011010 0x5a
B: 11100011 0xe3
&-------------------
C: 01000010 0x42
law : Any number of heels 0 Do position and , The result is 0, Any number of heels 1 Do position and , Keep the original value
Remember the application : It is generally used to assign a certain number to bit Positional clarity 0, And keep the other bits unchanged
for example : take 0x5a The second of this number 3 Positional clarity 0: 0x5a & 0xf7
76543210 Binary number
A:0x5a 01011010
B : 0xf7 11110111
C: 0x52 01010010
for example : take 0x5a The second of this number 3 Position and number 4 Positional clarity 0: 0x5a & 0xE7
76543210 Binary number
A:0x5a 01011010
B : 0xE7 11100111
C: 0x42 01000010
Bit or operator characteristic
Grammar format :C = A | B;
for example :
A: 01011010 0x5a
B: 11100011 0xe3
|-------------------
C: 11111011 0xfb
law : Any number of heels 1 Do a bit or , The result is 1, Any number of heels 0 Do a bit or , Keep the original value
Remember the application : It is generally used to assign a certain number to bit Location 1, And keep the other bits unchanged
for example : take 0x5a The second of this number 2 Location 1: 0x5a | 0x04
76543210 Binary number
A:0x5a 01011010
B : 0x04 00000100
C: 0x5e 01011110
for example : take 0x5a The second of this number 1,2 Location 1: 0x5a | 0x06
76543210 Binary number
A:0x5a 01011010
B : 0x06 00000110
C: 0x5e 01011110
Bitwise exclusive or operation characteristic :^
1. grammar :C = A ^ B;
for example :
A: 01011010 0x5a
B: 11100011 0xe3
^-------------------
C: 10111001 0xb9
2. law : Same as 0, Different for 1, It is used in the case of inversion
Bit inverse operator characteristic :~
1. grammar :C=~A;
for example :
A: 01011010 0x5a
~A:10100101 0xa5
2. law :0 change 1,1 change 0
3. Be careful : It's the opposite (~) The operators are general and bitwise AND (&) Combined to achieve a certain number of bit clear 0 operation
for example :
1.7 Shift Operators
<1>. function : Is to move the binary numbers left or right n A place ( Shift left for short , Move right )
<2>. Shift operator types : Two kinds of
Move left :A<<B
semantics : take A Move left B A place
Move right : A>>B
semantics : take A Move right B A place
for example :
1 << 3: take 1 Move left 3 position
3 >> 1: take 3 Move right 1 position
<3>. Shift operator features :
1. After moving to the left, the empty data on the right is used 0 Fill in
for example :A The data premise is char type
A=0x5A = 01011010
A << 2 = 01011010 << 2 The result is : 01101000 = 0x68
2. When an unsigned number is shifted to the right, the data left blank is used 0 Fill in
for example :01011010 >> 2 The result is :00010110 = 0x16
3. When a signed number is shifted to the right, the data left blank is filled with sign bits
for example :10100101( Premise is char type ) >> 2 The result is : 11101001 = 0xE9
4. Move left n Bits are equal to times 2 Of n Power
5. Move right n Bits are equal to dividing by 2 Of n Power
6. If the program involves multiplication or division in the future 2 Of n Secondary operation , Be sure to move left or right * perhaps / Of The execution efficiency is quite low !
for example :
int a = 1;
int b = a * 4; // Junk code
int b = a << 2; // High salary code
3. Whether it's a bitwise operator (&,|,^,~) Or move left , Moving them to the right does not change the value of the variable itself
for example :
int a = 3;
int b = a << 1;
printf("a = %d, b = %d\n", a, b); //a = 3, b = 6
1.8 Fetch address operator & Dereference operator *
<1>. Specify address characteristics : The address in the computer is determined by 32 Bit binary number composition , It's an address 32 position ,4 byte
<2>. Fetch address operator & effect : Get the first address of a variable in memory , Place holder %p Used to display address information
<3>. Fetch address operator & Use grammar format :& Variable name ;
for example :
int a = 250; // Distribute 4 Byte memory space and put a 250 Numbers
printf(" Variable a The first address of is %p\n", &a);
<4>. Dereference operator * effect : Get the data in memory according to the first address of the variable
Or write new data to the memory according to the first address of the variable
<5>. Dereference operator * Grammar is :* Address As a result, the memory can be manipulated
for example :
int a = 250;
printf("a The first address %p\n", &a);
printf("a The value of is %d\n", *&a); // Get the value of a variable by address
*&a = 520; // According to the variable a The address of a Of memory , Then inward Save and write a new number 520
1.9 Conditional operator ( Also known as the trinocular operator )
a) Grammar format : The whole expression result D = Conditional expression A ? Expression B : expression C
semantics : If A It's true ,D= expression B The result of the operation , otherwise D= expression C The result of the operation
for example :
int a = 1 ? 2 : 3; //a=2
1.10 Pay attention to the precedence of the operator
for example :
printf("%d\n", 2+3*2); //8
printf("%d\n", (2+3)*2); //10
Conclusion :() It has the highest priority , Not too much
for example :
int a;
a = 1 > 2 && 2 < 3;
Equivalent to :
a = (1 > 2) && (2 << 3);
边栏推荐
- 采用药丸屏的iPhone14或引发中国消费者的热烈抢购
- Deeply cultivate 5g, and smart core continues to promote 5g applications
- 飞桨EasyDL实操范例:工业零件划痕自动识别
- Benji Banas membership pass holders' second quarter reward activities update list
- [61dctf]fm
- 美国芯片傲不起来了,中国芯片成功在新兴领域夺得第一名
- PHP talent recruitment system development source code recruitment website source code secondary development
- 外盘期货平台如何辨别正规安全?
- How to set the WiFi password of the router on the computer
- Fleet tutorial 09 basic introduction to navigationrail (tutorial includes source code)
猜你喜欢
The survey shows that the failure rate of traditional data security tools in the face of blackmail software attacks is as high as 60%
How was the middle table destroyed?
【Web攻防】WAF检测技术图谱
国内首家 EMQ 加入亚马逊云科技「初创加速-全球合作伙伴网络计划」
The two ways of domestic chip industry chain go hand in hand. ASML really panicked and increased cooperation on a large scale
兰空图床苹果快捷指令
Error in composer installation: no composer lock file present.
Application of threshold homomorphic encryption in privacy Computing: Interpretation
【组队 PK 赛】本周任务已开启 | 答题挑战,夯实商品详情知识
Summary of methods for finding intersection of ordered linked list sets
随机推荐
Jarvis OJ 简单网管协议
[brush title] goose factory shirt problem
阈值同态加密在隐私计算中的应用:解读
Is it safe to open a securities account by mobile phone? Detailed steps of how to buy stocks
【刷題篇】鹅廠文化衫問題
网站页面禁止复制内容 JS代码
C# TCP如何设置心跳数据包,才显得优雅呢?
Error in composer installation: no composer lock file present.
Learnopongl notes (I)
Jarvis OJ webshell analysis
tf. sequence_ Mask function explanation case
Excuse me, is the redis syntax used in DMS based on the commands of the redis community version of the cloud database
How does win11 change icons for applications? Win11 method of changing icons for applications
Sentinel-流量防卫兵
Sentinel flow guard
养不起真猫,就用代码吸猫 -Unity 粒子实现画猫咪
BS-XX-042 基于SSM实现人事管理系统
【729. 我的日程安排表 I】
【 brosser le titre 】 chemise culturelle de l'usine d'oies
高数 | 旋转体体积计算方法汇总、二重积分计算旋转体体积