当前位置:网站首页>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 .
边栏推荐
- [open the door to the new world] see how the old bird of testing plays API testing between applause
- 扫雷简单版
- Alibaba Sentinel - 工作流程及原理解析
- Mysql复合查询(重要)
- 解析机器人与人类情感共鸣的主观意识
- 金山云回港上市:中国TO B云厂商的港股奔袭
- MySQL large table joint query optimization, large transaction optimization, avoiding transaction timeout, lock wait timeout and lock table
- Add a row to a specific location in the dataframe
- Weekly recommended short videos: how to make product development more effective?
- Verilog:阻塞赋值和非阻塞赋值
猜你喜欢

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

Weekly recommended short videos: how to make product development more effective?

C language: judging letters

第2章 VRP命令行

VASP calculation task error: M_ divide:can not subdivide 8 nodes by 6

centos安装mysql8

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

IDEA安装后无法启动

04 | 后台登录:基于账号密码的登录方式(上)

sqlilabs less-32~less-33
随机推荐
MYSQL入门与进阶(十三)
MySQL large table joint query optimization, large transaction optimization, avoiding transaction timeout, lock wait timeout and lock table
OWT server source code analysis (4) -- video module analysis of mixer out
《QA离业务代码能有多近?》通过codediff直接暴露缺陷
13_ue4进阶_蒙太奇动画实现一边走一边攻击
Plato Farm在Elephant Swap上铸造的ePLATO是什么?为何具备高溢价?
数仓中概念术语解析
Data truncation and estimation
Multithreading realizes concurrent reading and execution of multi use case files +selenium grid4 realizes distributed deployment of test framework
Analysis of concepts and terms in data warehouse
Zone --- line segment tree lazy marking board sub problem
C language: judging letters
Confusion matrix learning notes
vasp计算任务报错:M_divide:can not subdivide 8 nodes by 6
C陷阱与缺陷 第3章 语义“陷阱” 3.4 避免“举偶法”
第2章 VRP命令行
Notes on the ninth day
MySQL - count(字段)、count(主键)、count(1)、count(*)的区别
Chapter 2 VRP command line
金山云回港上市:中国TO B云厂商的港股奔袭