Comparison with zero value in C Language

Source: Internet
Author: User

1. Comparison between bool variables and zero values

The IF Statements for comparing bool values with zero values are as follows:

Bool btestflag = false;

A). If (btestflag = 0); If (btestflag = 1 );

B). If (btestflag = true); If (btestflag = false );

C). If (btestflag); If (! Btestflag );

 

Writing Method A: btestflag is easy to misunderstand as an integer variable. It does not mean that some compilers define false and the true values are all the same 0, 1. Therefore, this writing method is not good;

B's Writing Method: I think this method is feasible, but the bool type value should be assigned to btestflag value instead of integer value conversion, this may cause errors due to different compilers;

C: simple and clear, and achieve the desired effect. If btestflag is trueCode, Jump to the following when false.

 

2. Compare float variables with zero values

The IF statement for comparing float values with zero values is as follows:

Float ftestval = 0.0;

A). If (ftestval = 0.0); If (ftestval! = 0.0 );

B). If (ftestval >=- epsinon) & (ftestval <= epsinon); // epsinon is the defined precision.

 

 

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.