If statement and zero value comparison

Source: Internet
Author: User

Comparison of zero values:

1. bool type and zero value

Bool btestval;

It is better to write if (btestval) And if (! Btestval)

Instead of writing it as: If (btestval = 0) and if (btestval! = 0) // easy to misunderstand

Do not write it as follows: If (btestval = true) And if (btestval = false) // Some compilers treat true as-1, such as Visual Basic

 

2. Comparison between float variables and zero values

Float ftextval = 0.0;

If (ftextval> = 0.0-epsinon) & (ftextval <= 0.0 + epsinon) epsinon is the determined precision.

 

3. Comparison between NULL pointer and zero value

Int * P = NULL;

If (null = P) and if (null! = P ),Develop the habit of writing null at the beginning and variables at the end, because if a = sign is missing, the compiler will report an error. If it is written as if (P = NULL), the compiler will not report an error, it is very troublesome to check the error.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.