当前位置:网站首页>C traps and defects Chapter 3 semantic "traps" 3.7 evaluation order
C traps and defects Chapter 3 semantic "traps" 3.7 evaluation order
2022-07-29 03:03:00 【weixin_ Guest time】
Order of evaluation
The problem of operator priority is completely different from the evaluation order .
Operator precedence such as expression
a + b * c
It should be interpreted as
a + (b * c)
instead of
(a + b) * c
Such a rule . Evaluation order is another kind of rule , You can guarantee the following statements
if (count != 0 && sum/count < smallaverage) {
printf("average < %g\n", smallaverage);
}
Even when the variable count by 0 when , Nor will it produce a “ use 0 Do divisor ” Error of .
a < b && c < d
C The definition of language States a<b Should be evaluated first . If a It's really less than b, At this time, we must further study c<d evaluation , To determine the value of the entire expression . however , If a Greater than or equal to b, Then there is no need to c<d evaluation , The expression must be false .
in addition , Right a<b evaluation , The compiler may first correct a evaluation , Maybe first b evaluation , On some machines, it is even possible to evaluate them in parallel at the same time .
C There is only... In the language 4 Operators (&&、||、?: and ,) There is a specified order of evaluation . Operator || And operators && First, evaluate the left-hand operand , The right-hand operands are evaluated only if necessary . Operator ?: Yes 3 Operands : stay a?b:c in , Operands a First evaluated , according to a And then find the operand b or c Value . The comma operator first evaluates the left operand , then “ discarded ” This value , Then evaluate the right-hand operands .
Comma expressions that separate function parameters are not comma operators .f(x, y) in x and y The evaluation order of is uncertain ,g((x, y)) The evaluation order of is first x Value , Throw it away , Ask again y Value . function g There is only one parameter .
C The order in which all other operators in the language evaluate their operators is undefined . especially , Assignment operators do not guarantee any evaluation order .
Operator && And operators || It is very important to ensure that the inspection operations are carried out in the correct order , for example , In statement
if (y != 0 && x/y > tolerance)
conplain();
in , We must ensure that we do our best y Not 0 The time is right x/y evaluation .
//no guarantee
From an array x Before assignment in n Elements to arrays y The practice in is incorrect , Because it makes too many assumptions about the evaluation order :
i = 0;
while (i < n) {
y[i] = x[i++];
}
The problem lies in the code assumption above y[i] The address of will be at i The auto increment operation of is evaluated before execution , There is no guarantee of this !
//also no guarantee
The following version is written in a similar way , It's not true either :
i = 0;
while (i < n) {
y[i++] = x[i];
}
//can be guaranteed
The following way of writing works correctly :
i = 0;
while (i < n) {
y[i] = x[i];
i++;
}
I could just write it as :
for (i = 0; i < n; i++) {
y[i] = x[i];
}
边栏推荐
- C陷阱与缺陷 第3章 语义“陷阱” 3.8 运算符&&、||和!
- 01-sdram: Code of initialization module
- 数据截断及估计
- Typescript学习(一)
- MySQL - count(字段)、count(主键)、count(1)、count(*)的区别
- 明明开发薪资高,为什么我还是选了测试?
- C陷阱与缺陷 第3章 语义“陷阱” 3.6 边界计算与不对称边界
- 2. Nodejs -- path (\dirname, \filname), URL URL, querystring module, mime module, various paths (relative paths), web page loading (interview questions *)
- C陷阱与缺陷 第3章 语义“陷阱” 3.1 指针与数组
- 【npm错误】- npm ERR code ERESOLVE 和 npm ERR ERESOLVE could not resolve 问题
猜你喜欢

navicat新建数据库

【FreeSwitch开发实践】UniMRCP编译与安装

Plato Farm在Elephant Swap上铸造的ePLATO是什么?为何具备高溢价?

Zone --- line segment tree lazy marking board sub problem

Hangao database best practice configuration tool Hg_ BP log collection content

【打开新世界大门】看测试老鸟如何把API 测试玩弄在鼓掌之间

Engineering boy: under 20 years old, ordinary but not mediocre

2. Nodejs -- path (\dirname, \filname), URL URL, querystring module, mime module, various paths (relative paths), web page loading (interview questions *)

融云实时社区解决方案

MongoDB索引 (3)
随机推荐
sqlilabs less-32~less-33
明明开发薪资高,为什么我还是选了测试?
解读AI机器人养宠引领时尚潮流
数据截断及估计
C traps and defects Chapter 3 semantic "traps" 3.4 avoid "couple method"
What is SOA (Service Oriented Architecture)?
SQL查询数据之多表(关联)查询
Flask的创建的流程day05-06之创建项目
C陷阱与缺陷 第3章 语义“陷阱” 3.7 求值顺序
OSPF experiment
MySQL - count(字段)、count(主键)、count(1)、count(*)的区别
04 | 后台登录:基于账号密码的登录方式(上)
Navicat new database
Analysis of OWT server source code (III) -- video module analysis of mixer in
单例模式(饿汉式 懒汉式)
HTB-Blocky
Notes on the seventh day
金山云回港上市:中国TO B云厂商的港股奔袭
Day 10 notes
爆肝整理JVM十大模块知识点总结,不信你还不懂