当前位置:网站首页>Comparison between variable and "zero value"
Comparison between variable and "zero value"
2022-07-06 22:30:00 【It's Beichen not too PI acridine】
Variables and “ Zero value ” Comparison
bool Variables and “ Zero value ” Compare
bool Variables and “ Zero value ” To compare if How to write sentences ?
bool bTestFlag = FALSE;
Think about why bool Variables are generally initialized to FALSE better ?
You can make this boolean variable negative and non-zero , because FLASE Everyone knows the value of , In the compiler, it is defined as 0; but TRUE The value of ? All are 1 Do you ? Unfortunately , Not all of them 1.Visual C++ Defined as 1, And its siblings Visual Basic Just put TRUE Defined as -1. That's obvious FALSE Reverse is TRUE, but TRUE Not after inversion FLASE.
A), if(bTestFlag == 0);
if(bTestFlag == 1);
B), if(bTestFlag == TRUE);
if(bTestFlag == FLASE);
C), if(bTestFlag);
if(!bTestFlag);
Which group or groups are correct ? Let's analyze :
A) How to write it :bTestFlag What is it? ? Integer variables ? If it hadn't been for this name, it would have followed the previous naming conventions , I'm afraid it's easy to misunderstand it as an integer variable . So this kind of writing is not good .
B) How to write it :FLASE Everyone knows the value of , In the compiler, it is defined as 0; but TRUE The value of ? All are 1 Do you ? Unfortunately , Not all of them 1.Visual C++ Defined as 1, And its siblings Visual Basic Just put TRUE Defined as -1. That's obvious , This kind of writing is not good .
Everybody knows if Statements branch and jump by the value of the expression in parentheses behind them . If the expression is true , execute if The code immediately following the statement ; Otherwise, do not execute . That's obvious , This group is well written , It will not cause misunderstanding , Nor because TRUE or FLASE Error due to different defined values of . remember : Write code like this in the future .
Integer variables and “ Zero value ” Compare
Integer variables should be used as “==” or “!=” Direct and 0 Compare .
if (value == 0)
if (value != 0)
// Can't imitate the style of Boolean variables
if (value) // It's misleading value It's a boolean variable
if (!value)
float Variables and “ Zero value ” Compare
float Variables and “ Zero value ” To compare if How to write sentences ?
float fTestVal = 0.0;
A), if(fTestVal == 0.0);
if(fTestVal != 0.0);
B), if((fTestVal >= -EPSINON) && (fTestVal <= EPSINON)); //EPSINON For defined precision .
B Group is correct
Be sure to pay attention to , Whether it's float still double Variable of type , There are limits to accuracy . So you must avoid using floating-point variables with “==” or “!=” Compare it with numbers , We should try to translate it into “> =” or “ <=” form .
Pointer variables and “ Zero value ” Compare
Pointer variables should be used as “==” or “!=” And NULL Compare .
The zero value of a pointer variable is “ empty ”( Write it down as NULL). Even though NULL The value of is equal to 0 identical , But they have different meanings .
Suppose the name of the pointer variable is p, It's a standard to compare it to zero if The statement is as follows :
if (p == NULL) // p And NULL Explicit comparison , emphasize p It's a pointer variable
if (p != NULL)
Don't write as
if (p == 0) // It's easy to be misunderstood p It's an integer variable
if (p != 0)
perhaps
if (p) // It's easy to be misunderstood p It's a boolean variable
if (!p)
Sometimes we may see if (NULL == p) This weird format . It's not a program error , It's the programmer who wants to prevent if (p == NULL) Misspelled if (p = NULL), And intentionally put p and NULL Reverse . The compiler thinks if (p = NULL) It's legal. , But it will point out that if (NULL = p) It's wrong. , because NULL Cannot be assigned .
边栏推荐
- 硬件開發筆記(十): 硬件開發基本流程,制作一個USB轉RS232的模塊(九):創建CH340G/MAX232封裝庫sop-16並關聯原理圖元器件
- Aardio - 利用customPlus库+plus构造一个多按钮组件
- pytorch_YOLOX剪枝【附代码】
- Seata聚合 AT、TCC、SAGA 、 XA事务模式打造一站式的分布式事务解决方案
- 2500 common Chinese characters + 130 common Chinese and English characters
- 在IPv6中 链路本地地址的优势
- 如何用程序确认当前系统的存储模式?
- MySQL教程的天花板,收藏好,慢慢看
- Pit encountered by handwritten ABA
- config:invalid signature 解决办法和问题排查详解
猜你喜欢
【LeetCode】19、 删除链表的倒数第 N 个结点
signed、unsigned关键字
在IPv6中 链路本地地址的优势
Self made j-flash burning tool -- QT calls jlinkarm DLL mode
Web APIs DOM 时间对象
Memorabilia of domestic database in June 2022 - ink Sky Wheel
Aardio - 不声明直接传float数值的方法
基于 QEMUv8 搭建 OP-TEE 开发环境
将MySQL的表数据纯净方式导出
Clip +json parsing converts the sound in the video into text
随机推荐
重磅新闻 | Softing FG-200获得中国3C防爆认证 为客户现场测试提供安全保障
anaconda安装第三方包
pytorch_YOLOX剪枝【附代码】
UE4蓝图学习篇(四)--流程控制ForLoop和WhileLoop
Improving Multimodal Accuracy Through Modality Pre-training and Attention
中国VOCs催化剂行业研究与投资战略报告(2022版)
Dealing with the crash of QT quick project in offscreen mode
2022-07-05 stonedb sub query processing parsing time analysis
Notes de développement du matériel (10): flux de base du développement du matériel, fabrication d'un module USB à RS232 (9): création de la Bibliothèque d'emballage ch340g / max232 SOP - 16 et Associa
二分图判定
Data storage (1)
config:invalid signature 解决办法和问题排查详解
(18) LCD1602 experiment
2500 common Chinese characters + 130 common Chinese and English characters
Should novice programmers memorize code?
[leetcode] 19. Delete the penultimate node of the linked list
做国外LEAD2022年下半年几点建议
Clip +json parsing converts the sound in the video into text
Config:invalid signature solution and troubleshooting details
MySQL教程的天花板,收藏好,慢慢看