当前位置:网站首页>C traps and defects Chapter 2 syntax "traps" 2.6 problems caused by "hanging" else
C traps and defects Chapter 2 syntax "traps" 2.6 problems caused by "hanging" else
2022-07-29 03:02:00 【weixin_ Guest time】
“ Hang ”else Raised questions
if (x == 0)
if (y == 0) error();
else {
z = x + y;
f(&z);
}
The intention of programmers is that there should be two main situations :x be equal to 0 as well as x It's not equal to 0. about x be equal to 0, Unless y Also equal to 0( The function is called error), Otherwise, the program will not do any processing ; about x It's not equal to 0 The circumstances of , The program will first x And y The sum of is assigned to z, And then to z Call the function with the address of as a parameter f.
However , What this code actually does is far from the programmer's intention . The reason lies in C There are such rules in language , namely else Always match the nearest unmatched... In the same pair of parentheses if coordination .
The above program is actually implemented by logic to adjust the code indentation , As follows :
if (x == 0) {
if (y == 0) error();
else {
z = x + y;
f(&z);
}
}
in other words x It's not equal to 0, The program will not do anything . If you want to get the result of the programmer's intention reflected by the code indentation in the original example , It should be written like this :
if (x == 0) {
if (y == 0) {
error();
}
} else {
z = x + y;
f(&z);
}
The reason lies in the second if Has been parenthesized “ encapsulation ” up .
There are some C Programmers can achieve a similar effect by using macro definitions :
#define IF {if(
#define THEN ){
#define ELSE }else {
#define FI}}
such , In the previous example C The program can be written as :
IF x == 0
THEN IF y == 0
THEN error();
FI
ELSE z = x + y;
f(&z);
FI
amount to
{
if(x == 0)
{
{
if(y == 0)
{
error();
}
}
}else
{
z = x + y;
f(&z);
}
}
Another way is for everyone if The statement is equipped with a else, So it won't show up “ Hang ”else The problem of , But it will increase the difficulty of reading .
边栏推荐
- Zone --- line segment tree lazy marking board sub problem
- My approval function of conference OA project
- CentOS install mysql8
- 解读AI机器人养宠引领时尚潮流
- seed 随机种子
- Day 5 experiment
- Advanced architects, 16 common principles of microservice design and Governance
- .NET 序列化枚举为字符串
- MySQL 操作数据库数据报错:Fatal error encountered during command execution
- Redis configuration cache expiration listening event trigger
猜你喜欢

OSPF experiment

Multi table (Association) query of SQL query data

SQL查询数据之多表(关联)查询

Implementation principle of golang synergy

第09章_性能分析工具的使用

Verilog的时间系统任务----$time、$stime、$realtime

爆肝整理JVM十大模块知识点总结,不信你还不懂

SOA(面向服务架构)是什么?

2.nodejs--路径(_dirname,_filname)、url网址、querystring模块、mime模块、各种路径(相对路径)、网页的加载(面试题*)

2022-07-28 第四小组 修身课 学习笔记(every day)
随机推荐
C语言基础知识点汇总
SQL查询数据之多表(关联)查询
Summary of classic problems in Flink production environment
12_ue4进阶_换一个更好看的人物模型
Verilog: blocking assignment and non blocking assignment
navicat新建数据库
Confusion matrix learning notes
【机器人学习】机械臂抓手matlab运动学与admas动力学分析
融云 IM & RTC 能力上新盘点
百度副总裁李硕:数字技术加持下中国劳动力成本上升是好事
Pytest environment deployment + use case execution management + use case parameterization
C语言小项目 -- 通讯录(静态版+动态版+文件版)
MYSQL入门与进阶(十一)
sqlilabs less-32~less-33
.NET 序列化枚举为字符串
Wechat's crazy use of glide - life cycle learning
R语言ERROR: compilation failed for package ‘****‘
融云实时社区解决方案
【打开新世界大门】看测试老鸟如何把API 测试玩弄在鼓掌之间
C陷阱与缺陷 第3章 语义“陷阱” 3.6 边界计算与不对称边界