gpt4 book ai didi

c - C中 "=="运算符的返回值

转载 作者:太空狗 更新时间:2023-10-29 16:24:19 27 4
gpt4 key购买 nike

我是否可以假设在 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;
}

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

最佳答案

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?

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

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.

关于c - C中 "=="运算符的返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18097922/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com