C中'=='是什么意思?

编程入门 行业动态 更新时间:2024-10-10 13:13:49
本文介绍了C中'=='是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

==是什么意思,它和=有什么区别?

What is the meaning of == and how does it differ from =?

我怎么知道使用哪个?

推荐答案

== 是对相等性的测试.= 是一个赋值.

== 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

没有魔法.

更多推荐

C中'=='是什么意思?

本文发布于:2023-06-03 09:49:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/471183.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!