C中布尔表达式的返回值 [英] Return value of a boolean expression in C

查看:14
本文介绍了C中布尔表达式的返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于不值得一提的原因,我想知道布尔表达式是否有标准定义的值.例如

int foo () {返回 (bar > 5);}

背景是我担心我们的团队将 TRUE 定义为不同于 1 的东西,我担心有人可能会这样做:

if (foo() == TRUE) {/* 做事 */}

我知道最好的选择是简单地做

if (foo())

但你永远不知道.

是否有为布尔表达式定义的标准值,还是取决于编译器?如果有,标准值是否包含在 C99 中?C89呢?

解决方案

==, !=, &&等运算符code> 和导致布尔值的 || 将评估为表达式的 1 为真,如果表达式为假,则为 0.这个表达式的类型是int.

所以如果TRUE宏没有定义为1,如上面的比较就会失败.

在布尔上下文中计算表达式时,0 计算为假,非零计算为真.所以为了安全起见,TRUE 应该定义为:

#define TRUE (!0)

正如评论中提到的,如果您的编译器符合 C99,您可以 #include <stdbool.h> 并使用 truefalse.

根据C99:p>

6.5.3.3(一元算术运算符)

<块引用>

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

6.5.8(关系运算符)

<块引用>

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

6.5.9(等式运算符)

<块引用>

==(等于)和 !=(不等于)运算符是类似于关系运算符,除了它们的较低优先级.如果指定,则每个运算符都会产生 1关系为真,如果为假,则为 0.结果有类型int.

6.5.13(逻辑与运算符)

<块引用>

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

6.5.14(逻辑或运算符)

<块引用>

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

For reasons that are not worth mentioning, I want to know if there's a standard defined value for boolean expressions. E.g.

int foo () {
    return (bar > 5);
}

The context is that I'm concerned that our team defined TRUE as something different than 1, and I'm concerned that someone may do:

if (foo() == TRUE) { /* do stuff */ }

I know that the best option would be to simply do

if (foo())

but you never know.

Is there a defined standard value for boolean expressions or is it up to the compiler? If there is, is the standard value something included in C99? what about C89?

解决方案

An operator such as ==, !=, &&, and || that results in a boolean value will evaluate to 1 of the expression is true and 0 if the expression is false. The type of this expressing is int.

So if the TRUE macro is not defined as 1, a comparison such as the above will fail.

When an expression is evaluated in a boolean context, 0 evaluates to false and non-zero evaluates to true. So to be safe, TRUE should be defined as:

#define TRUE (!0)

As was mentioned in the comments, if your compiler is C99 compliant, you can #include <stdbool.h> and use true and false.

According to C99:

6.5.3.3 (Unary arithmetic operators)

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

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 1 if 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 1 if the specified relation is true and 0 if it is false. The result has type int.

6.5.13 (Logical AND operator)

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 (Logical OR operator)

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

这篇关于C中布尔表达式的返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆