Logic Operations 0 = x and x = 0

Source: Internet
Author: User

Labels: different C languages using Ar c Algorithm Application har

Many programs written by Daniel often see if (0 = x) {execution body}, while self-written programs often use if (x = 0) {execution body }. At the beginning, I was confident that this expression was equivalent, just to show off a distinctive style. When I read the C language calculation method, this part of the content will suddenly understand the comprehensive consideration of Daniel. The analysis process is as follows:

The combination of the "=" operator is the combination of the right (from the right to the left) "=" operator is the combination of the left.

0 = I the format of the error 0 = I and I = 0 is the same.

Do you understand this. If (0 = I) is written to prevent "=" from being written as "= ". If you accidentally write "=" as "="

If (x = 0) is used to write the statement, it is converted to If (x = 0). Even if an error is written, the compiler will not report an error. If it is written in the form of IF (0 = x), if (0 = X), the compiler returns an error. Error c2106: "=": The left operand must be the left value.

Write a program to test:

// Test_zeroeqx.cpp: defines the entry point of the console application. // # Include "stdafx. H "int _ tmain (INT argc, _ tchar * argv []) {int x = 0;/* Daniel's Writing Method */If (0 = X) printf ("x = 0"); else if (0! = X) printf ("X! = 0 ");/* when x = 0 is written as x = 0, the normal execution result of the program is x = 0 x! = 0 */If (x = 0) printf ("x = 0"); else if (X! = 0) printf ("X! = 0 ");/* remove the comment and you can see that x = 0 returns the false value * // * bool iszero; iszero = (x = 0); If (iszero) printf ("x = 0 returns false") else printf ("x = 0 returns false"); */getchar (); Return 0 ;}

 

From the test procedure, x = 0 returns false. If you write "=" accidentally "=", it is difficult to find errors if you follow the average person's instructions. Rather, the compiler reports an error directly.


Logic Operations 0 = x and x = 0

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.