C language entry classic error collection

In the process of just learning the C language, many beginners made almost the same mistakes, so I have sorted out the common classic mistakes for your reference. (Source of error: Mengxin Xiaodi Xuemei Group Speech)

The problem is continuously updated...

1. Mathematical comparison operator usage a<b<c

I want to compare the relationship between three numbers. For example, there are three numbers a, b, and c. It was naturally written in accordance with mathematical thinking a<b<c. As indicated by the error on the way.

But often like this, the results obtained are not the same as expected.

Let's take a look at the data. a=1, b=3, c=2. According to the original judgment a<b<c. The value of this formula should be false. But the result of the operation in C is indeed true.

Because the expression is evaluated from left to right, a<bthe operation is performed first . Obviously a is smaller than b. So the result is true. The value of true is 1. Then compare the result with c. 1<cObviously it is true, so the final result is true.

The correct wording should bea<b&&b<c

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_43058685/article/details/108819153