1 时(如果这不是一个一直存在的特性,就说 C99),我能否期望结果正好是 1 或只是一些非零值?这是否适用于所有 bool 运算符? 最佳答案 在 C99 §6.5.-6ren">
gpt4 book ai didi

c - 是>、<、!、&&、||的 "true"结果或 == 定义?

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

例如,当我用 C 语言编写 7>1 时(如果这不是一个一直存在的特性,就说 C99),我能否期望结果正好是 1 或只是一些非零值?这是否适用于所有 bool 运算符?

最佳答案

在 C99 §6.5.8 关系运算符中,第 6 项(<><=>=):

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) The result has type int.

至于相等运算符,在 §6.5.9(==!=)中更进一步:

The == (equal to) and != (not equal to) operators are analogous to the relational operators except for their lower precedence) Each of the operators yields 1 if 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.13 (&&) 中更进一步

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

...和§6.5.14(||)

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

以及一元算术运算符的语义!在 §6.5.3.3/4 结束:

The result of the logical negation operator ! is 0 if the value of its operand compares unequal to 0, 1 if the value of its operand compares equal to 0. The result has type int. The expression !E is equivalent to (0==E).

结果类型为 int一刀切,01作为可能的值。 (除非我错过了一些。)

关于c - 是>、<、!、&&、||的 "true"结果或 == 定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7687403/

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