== 符号的含义是什么?

发布于 2024-09-16 14:52:12 字数 145 浏览 6 评论 0原文

我想弄清楚 == 符号在这个程序中意味着什么?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

木格 2024-09-23 14:52:12

== 运算符测试相等性。例如:

if ( a == b )
    dosomething();

并且,在您的示例中:

x = y == z;

如果 y 等于 z,则 x 为 true (1)。如果 y等于 z,则 x 为假 (0)。

新手 C 程序员常犯的一个错误(以及一些非常有经验的程序员也会犯的错误)是:

if ( a = b )
    dosomething();

在本例中,b 被分配给 a,然后将其计算为布尔表达式。有时程序员会故意这样做,但这是不好的形式。另一个阅读代码的程序员不会知道它是有意(很少)还是无意(更有可能)。更好的构造是:

if ( (a = b) == 0 )   // or !=
    dosomething();

这里,将 b 赋值给 a,然后将结果与 0 进行比较。意图很明确。 (有趣的是,我曾与从未编写过纯 C 语言的 C# 程序员一起工作,因此无法告诉您这是做什么的。)

The == operator tests for equality. For example:

if ( a == b )
    dosomething();

And, in your example:

x = y == z;

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:

if ( a = b )
    dosomething();

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:

if ( (a = b) == 0 )   // or !=
    dosomething();

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.)

最后的乘客 2024-09-23 14:52:12

它是“等于”运算符。

在上面的示例中,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, if y is equal to z, x will be set to 1 (true), otherwise 0 (false). Because C (pre-C99) does not have a boolean type, the expression evaluates to an integer.

傲鸠 2024-09-23 14:52:12

平等。如果操作数相等则返回 1,否则返回 0。

Equality. It returns 1 if the operands are equal, 0 otherwise.

沦落红尘 2024-09-23 14:52:12

== 表示“等于”。该运算符的优先级高于=(等于)运算符。所以方程 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.

梦归所梦 2024-09-23 14:52:12
int main() 
{ 
    int x = 2, y = 6, z = 6;         
    x = y == z; 
    printf("%d", x); 
} 

让我们这样开始:

 x = (6==6)

它询问 6 等于 6?:true

x = true,但由于 x 是 int,所以 x= 1
x 的新值为 1。

打印以下内容:

1

int main() 
{ 
    int x = 2, y = 6, z = 6;         
    x = y == z; 
    printf("%d", x); 
} 

let`s start like this:

 x = (6==6)

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

陌上芳菲 2024-09-23 14:52:12

它说的是

X 将等于 true/1 或 false/0。

查看该行的另一种方式是:

x =  ( is y equal to true? then true/1 or false/0 )

It's saying

X will equal either true/1 or false/0.

another way to look at that line is this:

x =  ( is y equal to true? then true/1 or false/0 )
我一向站在原地 2024-09-23 14:52:12

== 运算符用于相等..
在你的例子中
如果 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

蓝戈者 2024-09-23 14:52:12

像这样思考:

= 意味着赋予某物一个值。

== 表示检查它是否等于某个值。

例如

int val = 5; //val is 5
//= actually changes val to 3
val = 3; 

//== Tests if val is 3 or not. 
//note: it DOES NOT CHANGE the value of val.
val == 3; 

int new_val = val == 3; //new_val will be 1, because the test is true

//the above statement is the same as
bool is_val_3 = false;
if( val == 3 )
   is_val_3 = true;
int new_val;
new_val = is_val_3;

//putting it together, 
val = new_val == 2; //sets val to 0. do you understand why now?

Think about it like this:

= means give something a value.

== means check if it is equal to a value.

For example

int val = 5; //val is 5
//= actually changes val to 3
val = 3; 

//== Tests if val is 3 or not. 
//note: it DOES NOT CHANGE the value of val.
val == 3; 

int new_val = val == 3; //new_val will be 1, because the test is true

//the above statement is the same as
bool is_val_3 = false;
if( val == 3 )
   is_val_3 = true;
int new_val;
new_val = is_val_3;

//putting it together, 
val = new_val == 2; //sets val to 0. do you understand why now?
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文