当前位置:网站首页>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];
}
边栏推荐
- Day 10 notes
- MySQL忘记密码怎么办
- 《QA离业务代码能有多近?》QA对业务代码进行可测性改造
- 第2章 VRP命令行
- MySQL 操作数据库数据报错:Fatal error encountered during command execution
- Verilog: blocking assignment and non blocking assignment
- C陷阱与缺陷 第3章 语义“陷阱” 3.4 避免“举偶法”
- Chapter 2 VRP command line
- Confusion matrix learning notes
- Zone --- line segment tree lazy marking board sub problem
猜你喜欢

会议OA项目之我的审批功能

R language error: compilation failed for package '****‘

看机器人教育引领素质教育主流

Algorithm --- paint the house (kotlin)

创客教育的起源和内涵的基本理念

VASP calculation task error: M_ divide:can not subdivide 8 nodes by 6
![[open the door to the new world] see how the old bird of testing plays API testing between applause](/img/79/1bc836cefef24d23e09d9865ff1fba.png)
[open the door to the new world] see how the old bird of testing plays API testing between applause

Verilog's time system tasks - $time, $stime, $realtime

Analysis of concepts and terms in data warehouse

解析机器人与人类情感共鸣的主观意识
随机推荐
VIM common commands
SQL查询数据之多表(关联)查询
OSPF experiment
C和指针 第3章 语义“陷阱” 3.5 空指针并非字符串
数据截断及估计
PHP process communication series (I) named pipes
Weekly recommended short videos: how to make product development more effective?
Verilog: blocking assignment and non blocking assignment
Interpreting AI robots' pet raising and leading fashion trends
C language small project - address book (static version + dynamic version + file version)
Chapter 09_ Use of performance analysis tools
MySQL compound query (important)
工科男生:20岁未满,平凡但不平庸
Idea replaces the contents of all files
Flink kernel source code (VII) Flink SQL submission process
融云实时社区解决方案
Day 10 notes
navicat新建数据库
JVM基础入门篇一(内存结构)
codeforces每日5题(均1500)-第二十五天