== 符号的含义是什么?
我想弄清楚 == 符号在这个程序中意味着什么?
int main()
{
int x = 2, y = 6, z = 6;
x = y == z;
printf("%d", x);
}
I am trying to figure out what == sign means in this program?
int main()
{
int x = 2, y = 6, z = 6;
x = y == z;
printf("%d", x);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
==
运算符测试相等性。例如:并且,在您的示例中:
如果 y 等于 z,则 x 为 true (1)。如果 y不等于 z,则 x 为假 (0)。
新手 C 程序员常犯的一个错误(以及一些非常有经验的程序员也会犯的错误)是:
在本例中,b 被分配给 a,然后将其计算为布尔表达式。有时程序员会故意这样做,但这是不好的形式。另一个阅读代码的程序员不会知道它是有意(很少)还是无意(更有可能)。更好的构造是:
这里,将 b 赋值给 a,然后将结果与 0 进行比较。意图很明确。 (有趣的是,我曾与从未编写过纯 C 语言的 C# 程序员一起工作,因此无法告诉您这是做什么的。)
The
==
operator tests for equality. For example:And, in your example:
x is true (1) if y is equal to z. If y is not equal to z, x is false (0).
A common mistake made by novice C programmers (and a typo made by some very experienced ones as well) is:
In this case, b is assigned to a then evaluated as a boolean expression. Sometimes a programmer will do this deliberately but it's bad form. Another programmer reading the code won't know if it was done intentionally (rarely) or inadvertently (much more likely). A better construct would be:
Here, b is assigned to a, then the result is compared with 0. The intent is clear. (Interestingly, I've worked with C# programmers who have never written pure C and couldn't tell you what this does.)
它是“等于”运算符。
在上面的示例中,
x
被分配了相等测试 (y == z
) 表达式的结果。因此,如果y
等于z
,则x
将设置为1
(true),否则0
(假)。由于 C(C99 之前的版本)没有布尔类型,因此该表达式的计算结果为整数。It is "equals" operator.
In the above example,
x
is assigned the result of equality test (y == z
) expression. So, ify
is equal toz
,x
will be set to1
(true), otherwise0
(false). Because C (pre-C99) does not have a boolean type, the expression evaluates to an integer.平等。如果操作数相等则返回 1,否则返回 0。
Equality. It returns 1 if the operands are equal, 0 otherwise.
== 表示“等于”。该运算符的优先级高于=(等于)运算符。所以方程 x = y == z;将尝试将 y==z 的结果分配给变量 x。在本例中为 1。
== means "is euual to". This operator has higher precedece than = (equal to) operator. So the equation x = y == z; will try to assign result of y==z to variable x. which is 1 in this case.
让我们这样开始:
它询问 6 等于 6?:true
x = true,但由于 x 是 int,所以 x= 1
x 的新值为 1。
打印以下内容:
1
let`s start like this:
It asks is 6 equivalent to 6?: true
x = true, but since x is an int, x= 1
The new value of x is 1.
The following is printed:
1
它说的是
查看该行的另一种方式是:
It's saying
another way to look at that line is this:
== 运算符用于相等..
在你的例子中
如果 y 等于 z 则 x 将具有 true 值,否则 x 将具有 false
== operator used for equality..
here in u r example
if y is equal to z then x will hav true value otherwise x will hav false
像这样思考:
= 意味着赋予某物一个值。
== 表示检查它是否等于某个值。
例如
Think about it like this:
= means give something a value.
== means check if it is equal to a value.
For example