In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article shares with you the content of sample analysis of various variables and zero comparisons in C language. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
1. Comparison between Bohr variable and Zero value
The bool type does not exist in the C language, but the bool type is added to the C99 standard.
The _ Bool type is introduced in the C99 standard, and the bool type is macrodefined in vs, while the _ Bool type makes a type designed by Microsoft not portable on other compilers, so we use the bool type here.
Next, let's get to our point:
Assuming that the Boolean variable name is flag, its comparison with the zero value is expressed in the if statement as follows:
If (flag) / / indicates that flag is true
If (! Flag) / / indicates that flag is false
In the C++ standard, the conversion rules between Boolean constants and integers and pointers are:
False- > 0 true- > 1
0-> false true- > non-0 value
However, the representation of ture can be different for different implementations, so we should avoid the following actions:
Int flag= 1 if (flag==ture) {;}
Therefore, we avoid using the judgment form when comparing bool types to zero in the future.
two。 Compare shaping variables with zero
Assuming that the integer variable is ret, the if statement that compares it to zero can be expressed as:
If (ret==0)
If (retinue 0)
It cannot be written in the style of bool.
If (ret) / / misleads people into thinking that ret makes bool type
If (! ret)
3. Comparison of floating-point variables and zero values
This is also the focus of our story today.
Here, let's look at a piece of code:
In our opinion, shouldn't x and y be the same? Why is there such a big difference here?
The existence and absence of floating point numbers are involved here.
Let's look at a piece of code:
Mathematically we think that the two numbers are not equal, but on a 32-bit calculator we think they are equal.
So if the absolute value of the difference between two floating-point numbers with the same sign is in the same range, we think that the two numbers are equal, otherwise the hormones are not equal. So how should we achieve it?
The code is as follows:
We introduce and two header files to achieve our requirements. Here we introduce the precision range of FLT_EPSILON to compare two float types. In addition to this precision range, there is also a DBL_EPSILON precision range used to compare double types. Here we will not give an example.
4. Comparison of pointer and zero value
The null of the pointer variable is NULL, that is, it does not point to any object, and although the values of NULL and 0 are the same, the meaning is different.
Suppose the pointer variable is p, and the if statement that compares it to zero is implemented as follows:
If (p==NULL) / / p is compared with NULL to display p to make pointer variables
If (paired null)
Try to avoid writing in the following form
If (p) / / is easily misunderstood as a variable of type bool
If (! P)
Or this.
If (paired variables 0) / / is easily misunderstood as a plastic type variable
If (pairing 0)
Thank you for reading! This is the end of this article on "sample analysis of all kinds of variables and zero value comparison in C language". I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.