更新时间:2022-06-22 02:02:43
==
是对相等性的测试.=
是一个赋值.
==
is a test for equality. =
is an assignment.
任何优秀的 C 语言书籍都应该涵盖这一点(我想在这本书的早期).
Any good C book should cover this (fairly early on in the book I would imagine).
例如:
int i = 3; // sets i to 3.
if (i == 3) printf("i is 3\n"); // prints it.
注意那些令人发指的:
if (i = 4) { }
这是有效的 C 并且经常被人发现.这实际上将分配 4 给变量i
并将其用作if
语句中的真值.这导致很多人使用更丑但更安全的方式:
which is valid C and frequently catches people out. This actually assigns 4 to the variable i
and uses that as the truth value in the if
statement. This leads a lot of people to use the uglier but safer:
if (4 == i) {}
如果你不小心使用了 =
而不是 ==
,这是一个编译时错误,而不是在你的程序运行时会咬你的东西:-)
which, if you accidentally use =
instead of ==
, is a compile-time error rather than something that will bite you on the backside while your program is running :-)
逻辑或运算符是两个竖线字符,一个接一个,不是一个字符.在这里,它与一个逻辑与和一个名为 b4
的变量对齐:
The logical-or operator is two vertical bar characters, one after the other, not a single character. Here it is lined up with a logical-and, and a variable called b4
:
||
&&
b4
没有魔法.