当前位置:网站首页>Operator priority, associativity, and whether to control the evaluation order [detailed explanation]
Operator priority, associativity, and whether to control the evaluation order [detailed explanation]
2022-06-26 05:39:00 【Holy cat】

Introduction to this issue
This paper mainly introduces : Three properties of operators : priority 、 associativity 、 Whether to control the order of evaluation ; And those that cannot be constrained by these three attributes , So as to determine the problem expression of the unique calculation path .
The properties of the operator
To evaluate a complex expression , You must be familiar with the properties of each operator in the expression . Operators have three properties :
1. The priority of the operator
2. The associativity of operators
3. Whether to control the order of evaluation
Be careful : Which of the two adjacent operands should be executed first , Depending on the priority of the operator , However, if two operators have the same priority, it depends on the associativity of the operators .
| priority | The operator | describe | Operation class | Usage examples | Result Type | associativity | Whether to control the order of evaluation |
|---|---|---|---|---|---|---|---|
| 1 | () | Aggregation group | N/A | ( expression ) | Same as expression | N/A | no |
| () | Function call | rexp(rexp,…,rexp) | rexp | L/R | no | ||
| [ ] | Subscript reference | rexp[rexp] | lexp | L/R | no | ||
| . | Access structure members | lexp. Member name | lexp | L/R | no | ||
| -> | Access structure pointer member | rexp-> Member name | lexp | L/R | no | ||
| 2 | ! | Logic is not | Monocular operations | ! rexp | rexp | R/L | no |
| ~ | According to the not | ~ rexp | rexp | R/L | no | ||
| ++ | Self increasing 1 | ++ lexp、lexp ++ | rexp | R/L | no | ||
| - - | Self reduction 1 | - - lexp、lexp - - | rexp | R/L | no | ||
| + | Monocular , Means to find a positive value | + rexp | rexp | R/L | no | ||
| - | Monocular , Means to find a negative value | - rexp | rexp | R/L | no | ||
| * | Indirect access to | * rexp | lexp | R/L | no | ||
| & | Address fetch | & lexp | rexp | R/L | no | ||
| ( Type name ) | Cast | ( Type name )rexp | rexp | R/L | no | ||
| sizeof | Find the number of bytes occupied | sizeof rexp、sizeof( type ) | rexp | R/L | no | ||
| 3 | *、/、% | ride 、 except 、 Integer remainder | Arithmetic operation | rexp * rexp、rexp / rexp、rexp % rexp | rexp | L/R | no |
| 4 | +、- | Add 、 reduce | rexp + rexp、rexp - rexp | rexp | L/R | no | |
| 5 | <<、>> | Shift left 、 Shift right | An operation | rexp << rexp、rexp >> rexp | rexp | L/R | no |
| 6 | <、<=、>、>= | Less than 、 Less than or equal to 、 Greater than 、 Greater than or equal to | Relationship between operation | rexp < rexp、rexp <= rexp、rexp > rexp、rexp >= rexp | rexp | L/R | no |
| 7 | ==、!= | be equal to 、 It's not equal to | rexp == rexp、rexp != rexp | rexp | L/R | no | |
| 8 | & | Bitwise AND | An operation | rexp & rexp | rexp | L/R | no |
| 9 | ^ | Bitwise XOR | rexp ^ rexp | rexp | L/R | no | |
| 10 | | | Press bit or | rexp | rexp | rexp | L/R | no | |
| 11 | && | Logic and | Logical operations | rexp && rexp | rexp | L/R | yes |
| 12 | || | Logic or | rexp || rexp | rexp | L/R | yes | |
| 13 | ?: | Conditional operation | Three mesh operation | rexp ? rexp:rexp | rexp | R/L | yes |
| 15 | =、+=、-=、*=、/=、%=、&=、^=、|=、<<=、>>= | The assignment operation | Binocular operation | lexp = rexp | rexp | R/L | no |
| 16 | , | Comma expression | Sequential operation | rexp,rexp | rexp | L/R | yes |
Be careful : In the table above rexp Is an R-value expression , Represents its value ;lexp It's an lvalue expression , Represents its memory space .N/A It means empty ,R/L It means from right to left ,L/R It means from left to right .
Problem expression
1. a * b + c * d + e * f
At first glance, there seems to be no problem , But in fact, the expression cannot be unique through priority and associativity Calculate the path , Because priority only determines which of the two adjacent operators executes first . Just like the expression a * b + c * d + e * f, We are not sure which multiplication to take first , May ask first a * b, Or maybe c * d, Or maybe e * d. If you find any multiplication, you will change the result of the other two multiplication , Then we cannot determine the unique value of the expression .
2. c + --c
The expression has a definite and unique calculation path , But I'm not sure c The value is obtained in –c Before or after execution , So the results are unpredictable , There is ambiguity .
3. Problem code
#include<stdio.h>
int fun()
{
static int count = 1;
return ++count;
}
int main()
{
int answer;
answer = fun() - fun() * fun();
printf("%d\n",answer);
return 0;
}
The same is true of this code , We can only determine the evaluation order after the function is called , However, it is impossible to determine the sequence of function calls by the priority of operators . The difference between who calls first and who calls later will inevitably lead to the failure of the calculated value , It is impossible to get exact results .
边栏推荐
- [arm] build boa based embedded web server on nuc977
- RIA ideas
- There are applications related to web network request API in MATLAB (under update)
- Ribbon负载均衡服务调用
- 1212312321
- Consul服务注册与发现
- The State Council issued a document to improve the application of identity authentication and electronic seals, and strengthen the construction of Digital Government
- 无线网络存在的安全问题及现代化解决方案
- skimage. morphology. medial_ axis
- Security problems in wireless networks and modern solutions
猜你喜欢

小小面试题之GET和POST的区别

Recursively traverse directory structure and tree presentation
![[red team] what preparations should be made to join the red team?](/img/03/f246f18f8925167dbd5e9d63912faa.png)
[red team] what preparations should be made to join the red team?

pytorch(网络模型)

Redis discovery bloom filter
![C# 40. Byte[] to hexadecimal string](/img/3e/1b8b4e522b28eea4faca26b276a27b.png)
C# 40. Byte[] to hexadecimal string

Introduction to GUI programming to game practice (I)
转帖——不要迷失在技术的海洋中

The difference between get and post in small interview questions

REUSE_ALV_GRID_DISPLAY 事件实现(DATA_CHANGED)
随机推荐
1212312321
Written before father's Day
A love that never leaves
【ARM】讯为rk3568开发板buildroot添加桌面应用
There are applications related to web network request API in MATLAB (under update)
Some doubts about ARP deception experiment
无线网络存在的安全问题及现代化解决方案
劣币驱逐良币的思考
AutowiredAnnotationBeanPostProcessor什么时候被实例化的?
Redis installation on Linux
睛天霹雳的消息
ZigBee learning in simple terms Lecture 1
cartographer_ fast_ correlative_ scan_ matcher_ 2D branch and bound rough matching
A new explanation of tcp/ip five layer protocol model
【ARM】在NUC977上搭建基于boa的嵌入式web服务器
【PHP】PHP二维数组按照多个字段进行排序
Two step processing of string regular matching to get JSON list
Sofa weekly | open source person - Yu Yu, QA this week, contributor this week
CMakeLists. txt Template
Official image acceleration