是“真实的”吗? >、<、!、&&、|| 的结果或 == 定义?

发布于 2024-12-08 10:35:07 字数 105 浏览 6 评论 0 原文

例如,当我用 C 语言编写 7>1 时(如果这不是一个始终存在的功能,则称为 C99),我是否可以期望结果恰好为 1 或者只是某个非零值?这适用于所有布尔运算符吗?

When I for instance write 7>1 in C (say C99 if this is not an always-been feature), can I expect the result will be exactly 1 or just some non-zero value? Does this hold for all bool operators?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

心凉怎暖 2024-12-15 10:35:07

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

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

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

==(等于)和 !=(不等于)运算符类似于关系运算符
运算符(优先级较低的运算符除外)如果满足以下条件,则每个运算符都会产生 1
指定关系为 true,如果为 false,则为 0。结果的类型为int。对于任意一对
操作数,恰好有一个关系为真。

逻辑 AND 和逻辑 OR 在 §6.5.13 (&&) 中更进一步

&&如果两个操作数比较不等于 0,则运算符应产生 1;否则,它
产生0。结果的类型为int

...和§6.5.14 (||)

||如果任一操作数与 0 比较不等于,则运算符应产生 1;否则,它
产生0。结果的类型为int

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

逻辑非运算符的结果!如果其操作数的值比较,则为 0
不等于 0,如果其操作数的值比较等于 0,则为 1。结果的类型为 int
表达式 !E 等价于 (0==E)。

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

In C99 §6.5.8 Relational Operators, item 6 (<,>,<= and >=):

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.

As for equality operators, it's a bit further in §6.5.9 (== and !=):

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.

The logical AND and logical OR are yet a bit further in §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.

... and §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.

And the semantics of the unary arithmetic operator ! are over at §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).

Result type is int across the board, with 0 and 1 as possible values. (Unless I missed some.)

盗琴音 2024-12-15 10:35:07

C 的布尔运算符遵循 Postel 法则:在你所做的事情上保持保守,自由地接受别人的东西。它将布尔表达式中的任何非零值视为 true,但它本身始终会生成 0 或 1。 2 != 3 始终为 1

C follows Postel's Law for its boolean operators: be conservative in what you do, be liberal in what you accept from others. It will treat any non-zero value as true in boolean expressions, but it will always produce either a 0 or a 1 itself. 2 != 3 is always 1.

淡水深流 2024-12-15 10:35:07

来自 ISO C99 标准,第 6.5.8 节:

6 每个运算符< (小于),> (大于),<=(小于
或等于),且 >=(大于或等于)应产生 1,如果
指定的关系为 true,如果为 false,则为 0。结果有
输入 int。

来自第 6.5.9 节:

3 ==(等于)和 !=(不等于)运算符类似于
关系运算符,但优先级较低。每个
如果指定的关系为真,则运算符的结果为 1;如果
这是错误的。结果的类型为 int。对于任意一对操作数,
其中一个关系恰好为真。

逻辑合取 (&&) 和析取 (||) 运算符也会发生同样的情况。

PS:顺便说一句,这就是为什么按位运算符(&|)通常可以用作逻辑运算符的非短路版本。

From the ISO C99 standard, section 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.

From section 6.5.9:

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

Same thing happens with the logical conjunction (&&) and disjunction (||) operators.

PS: Incidentally, this is why the bitwise operators (& and |) can usually be used as non-short-circuiting versions of the logical operators.

流殇 2024-12-15 10:35:07

所有产生逻辑真/假值的 C 运算符始终会产生 int 类型的结果,其中 false 值为 01< /code> 为真。

并非所有产生逻辑真/假值的 C表达式都是这种情况。例如, 中声明的 is*() 字符分类函数 (isdigit(), isupper( ) 等)如果条件为 false,则返回 0,但如果条件为 true,则可能返回任何非零值。

只要您直接使用结果作为条件:

if (isdigit(c)) ...
if (!isdigit(c)) ...
if (isdigit(c) || islower(c)) ...

并且不要尝试将其与某些内容进行比较:

if (isdigit(c) == 1) ...    /* WRONG */
if (isdigit(c) == true) ... /* ALSO WRONG */

这不会导致任何问题。

(您可以安全地将结果与0false进行比较,但没有充分的理由这样做;这就是!< /code> 运算符用于。)

All C operators that yield logically true/false values always yield a result of type int with the value 0 for false, 1 for true.

That's not the case for all C expressions that yield logically true/false values. For example, the is*() character classification functions declared in <ctype.h> (isdigit(), isupper(), etc.) return 0 if the condition is false, but may return any non-zero value if the condition is true.

As long as you use the result directly as a condition:

if (isdigit(c)) ...
if (!isdigit(c)) ...
if (isdigit(c) || islower(c)) ...

and don't attempt to compare it to something:

if (isdigit(c) == 1) ...    /* WRONG */
if (isdigit(c) == true) ... /* ALSO WRONG */

this shouldn't cause any problems.

(You can safely compare the result to 0 or false, but there's no good reason to do so; that's what the ! operator is for.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文