当前位置:网站首页>操作符的优先级、结合性、是否控制求值顺序【详解】
操作符的优先级、结合性、是否控制求值顺序【详解】
2022-06-26 05:31:00 【圣喵】

本期介绍
主要介绍:操作符的三种属性:优先级、结合性、是否控制求值顺序;以及那些无法通过该三条属性约束,从而确定唯一的计算路径的问题表达式。
操作符的属性
要计算一个复杂的表达式,就必须对熟悉该表达式中每个操作符的属性。操作符有三种属性:
1.操作符的优先级
2.操作符的结合性
3.是否控制求值顺序
注意:两个相邻的操作符先执行哪个,取决于操作符的优先级,但若两个操作符的优先级相同则取决于操作符的结合性。
| 优先级 | 操作符 | 描述 | 运算类 | 用法示例 | 结果类型 | 结合性 | 是否控制求值顺序 |
|---|---|---|---|---|---|---|---|
| 1 | () | 聚组 | N/A | (表达式) | 与表达式相同 | N/A | 否 |
| () | 函数调用 | rexp(rexp,…,rexp) | rexp | L/R | 否 | ||
| [ ] | 下标引用 | rexp[rexp] | lexp | L/R | 否 | ||
| . | 访问结构体成员 | lexp.成员名 | lexp | L/R | 否 | ||
| -> | 访问结构指针成员 | rexp->成员名 | lexp | L/R | 否 | ||
| 2 | ! | 逻辑非 | 单目运算 | ! rexp | rexp | R/L | 否 |
| ~ | 按位取反 | ~ rexp | rexp | R/L | 否 | ||
| ++ | 自增1 | ++ lexp、lexp ++ | rexp | R/L | 否 | ||
| - - | 自减1 | - - lexp、lexp - - | rexp | R/L | 否 | ||
| + | 单目,表示求正值 | + rexp | rexp | R/L | 否 | ||
| - | 单目,表示求负值 | - rexp | rexp | R/L | 否 | ||
| * | 间接访问 | * rexp | lexp | R/L | 否 | ||
| & | 取地址 | & lexp | rexp | R/L | 否 | ||
| (类型名) | 强制类型转换 | (类型名)rexp | rexp | R/L | 否 | ||
| sizeof | 求所占字节数 | sizeof rexp、sizeof(类型) | rexp | R/L | 否 | ||
| 3 | *、/、% | 乘、除、整数取余 | 算数运算 | rexp * rexp、rexp / rexp、rexp % rexp | rexp | L/R | 否 |
| 4 | +、- | 加、减 | rexp + rexp、rexp - rexp | rexp | L/R | 否 | |
| 5 | <<、>> | 左移位、右移位 | 位运算 | rexp << rexp、rexp >> rexp | rexp | L/R | 否 |
| 6 | <、<=、>、>= | 小于、小于等于、大于、大于等于 | 关系运算 | rexp < rexp、rexp <= rexp、rexp > rexp、rexp >= rexp | rexp | L/R | 否 |
| 7 | ==、!= | 等于、不等于 | rexp == rexp、rexp != rexp | rexp | L/R | 否 | |
| 8 | & | 按位与 | 位运算 | rexp & rexp | rexp | L/R | 否 |
| 9 | ^ | 按位异或 | rexp ^ rexp | rexp | L/R | 否 | |
| 10 | | | 按位或 | rexp | rexp | rexp | L/R | 否 | |
| 11 | && | 逻辑与 | 逻辑运算 | rexp && rexp | rexp | L/R | 是 |
| 12 | || | 逻辑或 | rexp || rexp | rexp | L/R | 是 | |
| 13 | ?: | 条件运算 | 三目运算 | rexp ? rexp:rexp | rexp | R/L | 是 |
| 15 | =、+=、-=、*=、/=、%=、&=、^=、|=、<<=、>>= | 赋值运算 | 双目运算 | lexp = rexp | rexp | R/L | 否 |
| 16 | , | 逗号表达式 | 顺序运算 | rexp,rexp | rexp | L/R | 是 |
注意:上表中的rexp是右值表达式,代表其值;lexp是左值表达式,代表其所在内存空间。N/A是空的意思,R/L是从右向左的意思,L/R是从左小右的意思。
问题表达式
1. a * b + c * d + e * f
乍一看似乎没什么问题,但其实该表达式是无法通过优先级和结合性来确定唯一的计算路径,因为优先级仅仅只能决定相邻的两个操作符谁先执行。就如表达式a * b + c * d + e * f,我们无法确定先求哪一个乘法,可能先求a * b,也可能c * d,也可能e * d。如若求任意一个乘法都会改变另外两个乘法的结果,那么我们就无法确定表达式唯一的值。
2. c + --c
该表达式有确定且唯一的计算路径,但无法确定c值的获取是在–c执行之前还是之后,所以结果是不可预测的,是存在歧义的。
3.问题代码
#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;
}
这个代码也是如此,我们只能确定函数调用完之后的求值顺序,但无法通过操作符的优先级来确定函数调用的先后顺序。谁先调用谁后调用的不同必然导致求出的值不通过,如此无法得到确切的结果的。
边栏推荐
- C# 39. Conversion between string type and byte[] type (actual measurement)
- SOFA Weekly | 开源人—于雨、本周 QA、本周 Contributor
- How to rewrite a pseudo static URL created by zenpart
- Supplementary course on basic knowledge of IM development (II): how to design a server-side storage architecture for a large number of image files?
- CMakeLists. txt Template
- Apktool tool usage document
- 无线网络存在的安全问题及现代化解决方案
- How to ensure the efficiency and real-time of pushing large-scale group messages in mobile IM?
- cartographer_ backend_ constraint
- 生命原来如此脆弱
猜你喜欢

How does P2P technology reduce the bandwidth of live video by 75%?

Leetcode513.找出树的左下角的值

cartographer_ optimization_ problem_ 2d

How to ensure the efficiency and real-time of pushing large-scale group messages in mobile IM?
![C# 40. Byte[] to hexadecimal string](/img/3e/1b8b4e522b28eea4faca26b276a27b.png)
C# 40. Byte[] to hexadecimal string

基于SDN的DDoS攻击缓解

【活动推荐】云原生、产业互联网、低代码、Web3、元宇宙……哪个是 2022 年架构热点?...

Command line interface of alluxio

递归遍历目录结构和树状展现

cartographer_local_trajectory_builder_2d
随机推荐
Implementation of IM message delivery guarantee mechanism (II): ensure reliable delivery of offline messages
cartographer_optimization_problem_2d
The parameter field of the callback address of the payment interface is "notify_url", and an error occurs after encoding and decoding the signed special character URL (,,,,,)
[arm] add desktop application for buildreoot of rk3568 development board
Baidu API map is not displayed in the middle, but in the upper left corner. What's the matter? Resolved!
Install the tp6.0 framework under windows, picture and text. Thinkphp6.0 installation tutorial
June 3 is a happy day
Tp5.0 framework PDO connection MySQL error: too many connections solution
Ad tutorial series | 4 - creating an integration library file
Chapter 9 setting up structured logging (I)
uniCloud云开发获取小程序用户openid
cartographer_local_trajectory_builder_2d
Learn cache lines and pseudo sharing of JVM slowly
劣币驱逐良币的思考
cartographer_pose_graph_2d
Introduction to GUI programming to game practice (II)
CMakeLists. txt Template
Redis usage and memory optimization
Supplementary course on basic knowledge of IM development (II): how to design a server-side storage architecture for a large number of image files?
bingc(继承)