当前位置:网站首页>[arithmetic, relation, logic, bit, compound assignment, self increasing, self decreasing and other] operators (learning note 4 -- C language operators)
[arithmetic, relation, logic, bit, compound assignment, self increasing, self decreasing and other] operators (learning note 4 -- C language operators)
2022-06-13 01:47:00 【It's Beichen bupiacra】
Preface :
If you are learning C Language without knowing where to start , You can study with me C Language , During the winter vacation, I will post a blog every day , There are all kinds of C The knowledge of language , If you want to learn , Want to improve , Come and punch in with me every day , I hope we can make progress together .
C Language operators
C Language operators can be seen from the number of logical numbers , Can be divided into one item 、 Two eyes 、 Ternary operator .
One 、 Arithmetic operator
| Operator | effect | Example | result |
|---|---|---|---|
| + | Add the left and right operands | 7+4 | 11 |
| - | Subtract the left and right operands | 7-4 | 3 |
| * | Multiply the left and right operands | 7*4 | 28 |
| / | Divide the left and right operands | 7/4 | 1 |
| % | Modulo the left and right operands ( Seeking remainder ) operation | 7%4 | 3 |
These five arithmetic operators are binary operators , The function of arithmetic operators is to evaluate .
Multiplication 、 division 、 Modulo operators take precedence over addition 、 Subtraction has high priority .
1. Implicit conversion
When performing arithmetic operations , In the left and right operands , The operation will be based on the largest data type , That is, the smaller data type will be converted to the larger data type first , And then we do the calculation . The process of automatically converting small types to large types , We call it “ Implicit type conversion ”.
Here are some common :
char < int < float < double
7 / 4 = 1
7.0 / 4 = 1.75
7 / 4.0 = 1.75
2. In division operations , The divisor cannot be zero 0
If the divisor is 0, You get the wrong results .
3. In modulo operation, both operands should be integers
And only the left operand will affect the positive and negative relationship of the result , That is, if the left operand is positive , Then the module result is also positive . conversely , If the left operand is negative, the modulo result is also negative .
7 % -4 = 3
-7 % 4 = -3
Two 、 Relational operator
| Operator | effect | Example | result |
|---|---|---|---|
| == | Compare the left and right operands for equality | 5 == 8 | 0 |
| != | Compare whether the left and right operands are not equal | 5 != 8 | 1 |
| < | Compare whether the left operand is less than the right operand | 5 < 8 | 1 |
| <= | Compare whether the left operand is less than or equal to the right operand | 5 <= 8 | 1 |
| > | Compare whether the left operand is greater than the right operand | 5 > 8 | 0 |
| >= | Compare whether the left operand is greater than or equal to the right operand | 5 >= 8 | 0 |
The above relational operators are binary operators , To compare the size relationship between the left and right operands .
C Language use 1 To show the truth , use 0 To show false . But the true value is not just 1, But any non-zero value . It's just that they usually use 1 To show the truth .
3、 ... and 、 Logical operators
| Operator | type | effect | Example | result |
|---|---|---|---|---|
| ! | Monocular | Get a logical value of the inverted operand | ! 0(! 1) | 1(0) |
| && | Binocular | When both the left and right logical values are true , The result is true , All other cases are false | 1&&1 (0&&1、1&&0、0&&0) | 1(0) |
| || | Binocular | When both the left and right logical values are false , The result is false , The results of other cases are all true | 0||1、1||0、1||1 (0||0) | 1(0) |
1. Short circuit effect
When using logical and operators , If the result of the left operand is false , The direct return result is false , Instead of checking the right operand .
When using logical or operators , If the result of the left operand is true , The direct return result is true , Nor will we check the right operand .
Four 、 An operator
| Operator | name | effect | type |
|---|---|---|---|
| << | Bitwise shift left operator | Turn all positions to the left ( High position ) Move , Low complement 0 | Binocular |
| >> | Bitwise shift right operator | Turn all positions to the right ( Low position ) Move , The high bit is mandatory for unsigned types 0, For signed types, the sign bit is continued | Binocular |
| ~ | Bitwise negation operator | Invert all bits (1 Turn into 0,0 Turn into 1) | Monocular |
| & | Bitwise and operator | And operate the two operands bit by bit | Binocular |
| | | bitwise or operator | To perform a bitwise OR operation on two operands | Binocular |
| ^ | bitwise exclusive or operator | Bitwise exclusive or of two operands | Binocular |
Logical operators operate on operands , Bit operators operate on the binary bits of operands .
The following pairs of integers 23, Its 8 Bit binary code is 0001 0111
23<<1
have to 0010 1110
23>>1
have to 0000 1011
~23
have to 1110 1000
23&50
have to 0001 0010
23|50
have to 0011 0111
23^50
have to 0010 0101
( Exclusive or : Two different , One for 1, One for 0, The result is 1)
5、 ... and 、 Compound assignment operator
| Operator | effect | Example | amount to |
|---|---|---|---|
| += | Add the left variable value to the right operand and assign it to the left variable | a += 1 | a = a + 1 |
| -= | The left variable value is subtracted from the right operand and then assigned to the left variable | a -= 1 | a = a - 1 |
| *= | The left variable value is multiplied by the right operand and then assigned to the left variable | a *= 1 | a = a * 1 |
| /= | Divide the value of the left variable by the right operand and assign it to the left variable | a /= 1 | a = a / 1 |
| %= | The left variable value and the right operand are modulo assigned to the left variable | a %= 1 | a = a % 1 |
| <<= | Move the left variable value to the left bit by bit, and assign the value to the left variable after positioning the right operand | a <<= 1 | a = a << 1 |
| >>= | Shift the value of the left variable to the right bit by bit, and assign the value to the left variable after positioning the right operand | a >>= 1 | a = a >> 1 |
| &= | The left variable value and the right operand are bitwise summed, and then assigned to the left variable | a &= 1 | a = a & 1 |
| |= | Assign the left variable value to the left variable after bitwise or with the right operand | a |= 1 | a = a | 1 |
| ^= | Assign the left variable value to the left variable after bitwise exclusive or with the right operand | a ^= 1 | a = a ^ 1 |
These operators can only be used with modifiable variables , Not available for constants .
6、 ... and 、 Operators with side effects
Assignment operator 、 Compound assignment operator 、 Self increasing 、 Self - subtracting operator .
We usually call these behaviors that can change operands “ side effect ”, Operators with such behavior are called “ Operators with side effects ”.
7、 ... and 、 Self increasing 、 Self - subtracting operator
It's a unary operator , It is also an operator with side effects .
a The initial value for the 1
| Operator | form | effect | Example | result |
|---|---|---|---|---|
| ++ | Prefix self incrementing | Add the operands to 1 And returns the new value of the operand | ++a | 2 |
| ++ | Suffixes increase | Add the operands to 1 And returns the original value of the operand | a++ | 1 |
| - - | The prefix subtracts itself | Subtract... From the operands 1 And returns the new value of the operand | - -a | 0 |
| - - | Suffix subtraction | Subtract... From the operands 1 And returns the original value of the operand | a- - | 1 |
If we simply want the operands to add 1, Instead of using this new value , No matter the prefix or suffix autoincrement operators are used ; On the contrary, if you need to use this new value , There is a difference between prefix and suffix .
8、 ... and 、 Other operators
1. Type conversion operators ()
The behavior of automatically converting small types to large types is implicit type conversion .
Now let's talk about the display type conversion , It can not only convert a small type to a large type like implicit type conversion , You can also convert a large type to a small type , This cannot be done by implicit type conversion .
( data type ) Operands
The data type in parentheses indicates the destination data type to be converted .
2. The comma operator
int a;
a =(3,4,5);
The result is 5
int a;
a = 3,4,5;
The result is 3
3. Conditional operator
It's a trinary operator
Operands 1 ? Operands 2 : Operands 3
If the operands 1 It's true , Returns the operand 2, Otherwise, return the operand 3.
4.sizeof Operator
sizeof ( Operands );
This operand can be a data type , It can also be a constant or variable of some data type .
If operand is a data type , Then you must use parentheses , If the operand is not a data type , You can omit the parentheses .
sizeof Operator to return its size , In bytes . adopt sizeof Operator can easily know the amount of space occupied by a data type in memory .
边栏推荐
- 30: Kakfa simulates JSON data generation and transmission
- [official document summary] writing standards for academic dissertations of National University of science and technology
- Delphi Google API text to speech MP3 file
- Devaxpress Chinese description --tcximagelist (enhanced image list control)
- Plumber game
- matplotlib画图中文乱码
- Run Presto under docker to access redis and Bi presentation
- 水管工遊戲
- Temporary objects and compilation optimization
- 六、出库管理功能的实现
猜你喜欢

万字讲清 synchronized 和 ReentrantLock 实现并发中的锁

Temporary objects and compilation optimization

Alertwindowmanager pop up prompt window help (Part 1)

六、出库管理功能的实现

谷歌加大型文字广告是什么?怎么用?

Using OpenCV in go

Devaxpress Chinese description -- tdxgallerycontrol object (gallery component)

四、入库管理功能的完善

DFS and BFS to solve Treasure Island exploration

如何通过受众群体定位解决实际问题?
随机推荐
[WSL2]WSL2迁移虚拟磁盘文件ext4.vhdx
TensorFlow 2.x 多显卡分布式训练
Introduction to common ROS commands
leetcode743. 网络延迟时间(中等, dijkstra)
How to solve practical problems through audience positioning?
Restful interface specification annotation of pringboot (2)
[wsl2] restrict wsl2 accessible hardware resources (cpu/ memory)
Unity JsonUtility 无法序列化List
Use mediapipe+opencv to make a simple virtual keyboard
Introduction to Google unit testing tools GTEST and gmoke
Padavan mounts SMB sharing and compiles ffmpeg
Using atexit to realize automatic destruct of singleton mode
Implementation of pointer linked list
Devaxpress Chinese description --tcxpropertiesstore (property store recovery control)
What is Google plus large text ads? How to use it?
四、入库管理功能的完善
About tkinter Canvas does not display pictures
【官方文件汇总】国科大学位论文撰写规范
Magics 23.0 how to activate and use the slice preview function of the view tool page
Wsl2 + vcxsrv + opengl3.3 configuration