C语言 C中“==”运算符的返回值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18097922/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:05:12  来源:igfitidea点击:

Return value of "==" operator in C

c

提问by Victor Dodon

Can I assume that in C, the "==" operator will always evaluate to 1 if the two values are equal or it can evaluate to other "true" values?

我可以假设在 C 中,如果两个值相等,“==”运算符将始终计算为 1,或者它可以计算为其他“真”值?

struct ss {
    int id;
};

struct os {
    int sid;
    int state;
};

int count(struct ss *s, int state)
{
    int num = 0;
    // foreach o (of type os*) in a hash table
        num += o->state == state && (s ? o->sid == s->id : 1);

    return num;
}

So o->sid == s->idwill return always 1 or 0, or it can return other values?

那么o->sid == s->id将始终返回 1 或 0,还是可以返回其他值?

回答by Yu Hao

Can I assume that in C, the "==" operator will always evaluate to 1 if the two values are equal or it can evaluate to other "true" values?

我可以假设在 C 中,如果两个值相等,“==”运算符将始终计算为 1,或者它可以计算为其他“真”值?

Yes, and so does !=><>=<=all the relational operator.

是的,!=><>=<=所有关系运算符也是如此。

C11(ISO/IEC 9899:201x) §6.5.8 Relational operators

C11(ISO/IEC 9899:201x) §6.5.8关系运算符

Each of the operators < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to) shall yield 1 if the specified relation is true and 0 if it is false.107) The result has type int.

如果指定的关系为真,则每个运算符 <(小于)、>(大于)、<=(小于或等于)和 >=(大于或等于)应产生 1,如果是,则产生 0 false.107) 结果的类型为 int。

回答by Pierre Fourgeaud

From the standard :

从标准:

6.5.8 Relational operators

Each of the operators < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to) shall yield 1if the specified relation is true and 0 if it is false. The result has type int.

6.5.9 Equality operators

The ==(equal to) and != (not equal to) operators are analogous to the relational operators except for their lower precedence. Each of the operators yields 1if the specified relation is true and 0 if it is false. The result has type int. For any pair of operands, exactly one of the relations is true.

6.5.8 关系运算符

如果指定的关系为真,则每个运算符 <(小于)、>(大于)、<=(小于或等于)和 >=(大于或等于)应产生1,如果是,则产生 0错误的。结果的类型为int

6.5.9 等式运算符

==(等于),!=(不等于)运算符类似于关系运算符除了他们的优先级低。如果指定的关系为真,则每个运算符产生 1,如果为假,则产生 0。结果的类型为int。对于任何一对操作数,只有一个关系为真。

For logical operands (&&, ||) :

对于逻辑操作数 ( &&, ||) :

6.5.13 Logical AND operator ( or 6.5.14 Logical OR operator )

The &&(or ||) operator shall yield 1 if both of its operands compare unequal to 0; otherwise, it yields 0. The result has type int.

6.5.13 逻辑 AND 运算符(或 6.5.14 逻辑 OR 运算符)

&&(或||)操作者应得到1,如果两个操作数的比较不等于0; 否则,结果为 0。结果的类型为 int。

You can check the last draft here : http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf

你可以在这里查看最后的草稿:http: //www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf

Conclusion :

结论 :

  • All the equality and relational operator (==, !=, <, >, <=, >=) return 0for falseand 1for true.

  • The logical operators (==, ||, !) treat 0as falseand other values as truefor their operands. They also return 0as falseand 1as true.

  • 所有等式和关系运算符 ( ==, !=, <, >, <=, >=) 都返回0forfalse1for true

  • 逻辑运算符 ( ==, ||, !) 将0asfalse和其他值视为true其操作数。它们也返回0asfalse1as true

回答by Jonathan Leffler

The comparison (equality and relational) operators (==, !=, <, >, <=, >=) all return 0 for false and 1 for true — and no other values.

比较(相等和关系)运算符 ( ==, !=, <, >, <=, >=) 都返回 0 表示 false 和 1 表示真 - 并且没有其他值。

The logical operators &&, ||and !are less fussy about their operands; they treat 0 as false and any non-zero value as true. However, they also return only 0 for false and 1 for true.

逻辑运算符&&,||!对其操作数不那么挑剔;他们将 0 视为假,将任何非零值视为真。但是,它们也只返回 0 表示假,返回 1 表示真。

回答by Andreas Fester

Can I assume that in C, the "==" operator will always evaluate to 1 if the two values are equal or it can evaluate to other "true" values?

我可以假设在 C 中,如果两个值相等,“==”运算符将始终计算为 1,或者它可以计算为其他“真”值?

Yes, for a standard compliant compiler, this assumption is correct:

是的,对于符合标准的编译器,这个假设是正确的:

Programming languages — C, §6.5.9 Equality operators(http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf):

编程语言 - C,§6.5.9 相等运算符http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf):

Each of the operators yields 1 if the specified relation is true and 0 if it is false. The result has type int.

如果指定的关系为真,则每个运算符产生 1,如果为假,则产生 0。结果的类型为 int。