Based on the relevant knowledge points in the high quality C++/C Programming Guide.
Comparison of Boolean variables with 0 values
"Rule 4-3-1" does not compare Boolean variables directly with TRUE, FALSE, or 1, 0.
Depending on the semantics of the Boolean type, the value of 0 is False (as false), and any non-0 value is True (it is recorded as true). The value of TRUE is exactly what does not have a uniform standard. For example, Visual C + + defines true as 1, while visual Basic defines true as-1. Assuming that the Boolean variable is named flag, the standard if statement that compares it to the 0 value is as follows:
if (flag)//indicates that flag is true
if (!flag)//indicates flag is false
Other uses are bad styles, such as:
if (flag = = TRUE)
if (flag = = 1)
if (flag = = FALSE)
if (flag = = 0)
Self-summary: That is, the Boolean value itself represents the real and home, do not need to judge the comparison.
Two, integer variable and 0 value comparison
"Rule 4-3-2" should use the integer variable with "= =" or "! = "Compare directly with 0.
Assuming that the integer variable is named value, the standard if statement that compares it to the 0 value is as follows:
if (value = = 0)
if (value! = 0)
Cannot imitate the style of a Boolean variable and write it
if (value)//can make people misunderstand that value is a Boolean variable
if (!value)
Self-summary: The integer variable is a variable and does not represent a certain value, if the direct representation of 1 or nonzero will cause an error, unable to achieve the effect.
Three, floating-point variable and 0 value comparison
"Rule 4-3-3" cannot use a floating-point variable with "= =" or "! = "Compared to any number.
Be aware that there is a precision limit for variables of either float or double type. So be sure to avoid using a floating-point variable with "= =" or
“! = "In comparison with numbers, you should try to convert them into" >= "or" <= "forms.
Assuming that the name of the floating-point variable is x, you should
if (x = = 0.0)//Comparison of implied errors
Translates to
if ((X>=-epsinon) && (X<=epsinon))
Where Epsinon is the allowable error (i.e. precision).
Self-summary: The floating-point variable and the 0 value comparison will be compared with about 0 in the error range of the data field, or there will be a precision deviation.
Four, pointer variable and 0 value comparison
"rule 4-3-4" should use the pointer variable "= =" or "! = "Compared to NULL. The 0 value of the
pointer variable is "EMPTY" (marked as null). Although the value of NULL is the same as 0, the meaning of the two is different. Assuming that the pointer variable is named p, the standard if statement that compares to the 0 value is as follows:
if (p = = N ULL)//P with NULL Explicit comparison, emphasizing p is pointer variable
if (P! = NULL)
do not write
I F (p = = 0)//easy to misunderstand p is an integer variable
if (P! = 0) or
if (p)// Easy to misunderstand p is a Boolean variable
if (!p)
Self-Summary: The 0 value of the pointer variable is NULL, and vice versa is non-null.
Compared with the above four types and 0 values, it shows the importance of writing the specification, if it is written according to your own idea, it will make the reader difficult to read, and will cause coding error. For a good code environment, you should strictly follow the code requirements.
Summary of each type vs. 0 comparison