C语言中布尔表达式的返回值 [英] Return value of a boolean expression in C
问题描述
出于不值得一提的原因,我想知道布尔表达式是否有标准定义的值.例如
int foo () {
return (bar > 5);
}
上下文是,我担心我们的团队将TRUE定义为不同于1的东西,并且担心有人可能会这样做:
if (foo() == TRUE) { /* do stuff */ }
我知道最好的选择就是简单地做
if (foo())
但是你永远不知道.
是否为布尔表达式定义了标准值,还是由编译器决定?如果存在,则C99中是否包含标准值? C89呢?
运算符,例如==
,!=
,&&
和||
,其结果为布尔值,其值将为表达式的1为true,如果表达式为false,则为0.这种表达的类型是int
.
因此,如果TRUE
宏未定义为1
,则上述比较将失败.
在布尔上下文中评估表达式时,0评估为false,非零评估为true.为安全起见,TRUE
应该定义为:
#define TRUE (!0)
如评论中所述,如果您的编译器符合C99,则可以#include <stdbool.h>
并使用true
和false
.
根据 C99 :>
6.5.3.3(一元算术运算符)
如果 其操作数的值不等于0,如果其值等于1 操作数比较等于0.结果的类型为
int
.这 表达式!E
等同于(0==E)
.
6.5.8(关系运算符)
每个运算符
<
(小于),>
(大于),<=
(小于或等于)和>=
(大于或等于) 如果指定的关系为true,则应产生1,如果为false,则应产生0. 结果的类型为int
.
6.5.9(等于运算符)
==
(等于)和!=
(不等于)运算符是 与关系运算符类似,除了它们的下限 优先.如果指定了每个运算符,则得出1 关系为true,如果为false,则为0.结果具有类型int
.
6.5.13(逻辑AND运算符)
如果
&&
运算符的两个操作数都比较,则应产生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 typeint
. 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 typeint
.
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 typeint
.
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 typeint
.
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 typeint
.
这篇关于C语言中布尔表达式的返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!