while(foo) vs while(foo != NULL) [英] while(foo) vs while(foo != NULL)
问题描述
有人能解释一下 while(foo)
和 while(foo != NULL)
是如何等效的吗?还有:
Can someone explain how while(foo)
vs while(foo != NULL)
are equivalent? Also:
while(!foo)
vs while(foo == NULL)
我知道!不是,但这就是我所知道的.
I know that ! is not, but that is about all I know.
推荐答案
假设 foo
是指针类型,while (foo)
和 while (foo != NULL)
完全等效.两者也等价于 while (foo != 0)
.(在我看来 while (foo != NULL)
更清楚地表达了意图.)
Assuming foo
is of pointer type, while (foo)
and while (foo != NULL)
are exactly equivalent. Both are also equivalent to while (foo != 0)
. (In my opinion while (foo != NULL)
more clearly expresses the intent.)
在任何需要条件的上下文中(if()
、while()
、其他一些),表达式都被视为 true如果条件比较不等于零,假如果比较等于零.
In any context requiring a condition (if()
, while()
, a few others), the expression is treated as true if the condition compares unequal to zero, false if it compares equal to zero.
NULL
是一个宏,它扩展为实现定义的空指针常量.将指针值与 NULL
进行比较,将为空指针生成真值,为任何非空指针生成假值.
NULL
is a macro that expands to an implementation-defined null pointer constant. Comparing a pointer value to NULL
yields a true value for a null pointer, a false value for any non-null pointer.
常量0
是一个空指针常量[*].(这并不意味着空指针具有 0x00000000
或类似的相同位表示,尽管它经常这样做;这意味着常量 0
in源代码 可以表示空指针.)正如您所料,将指针值与空指针常量进行比较会告诉您该指针是否为空指针.在 while (foo)
中,与零的比较是隐式的——但它仍然测试 foo
是否为空指针.
The constant 0
is a null pointer constant [*]. (This doesn't mean that a null pointer has the same bit representation of 0x00000000
or something similar, though it often does; it means that a constant 0
in source code can denote a null pointer.) As you'd expect, comparing a pointer value to a null pointer constant tells you whether that pointer is a null pointer or not. In while (foo)
, the comparison to zero is implicit -- but it still tests whether foo
is a null pointer or not.
更一般地,while (foo)
将 foo
与 0
进行比较,相当于将其与适当类型的零"进行比较.while (foo)
总是等价于 while (foo != 0)
.对于浮点值,它也等价于 while (foo != 0.0)
.对于字符值,它等价于 while (foo != '\0')
.而且,正如我们所见,对于一个指针值,它等价于 while (foo != NULL)
.
More generally, while (foo)
compares foo
to 0
, which is equivalent to comparing it to "zero" of the appropriate type. while (foo)
is always equivalent to while (foo != 0)
. For a floating-point value, it's also equivalent to while (foo != 0.0)
. For a character value, it's equivalent to while (foo != '\0')
. And, as we've seen, for a pointer value, it's equivalent to while (foo != NULL)
.
(在 C 中,如果条件为假,比较运算符总是产生 0
的 int
值,如果条件为真则产生 1
--但任何非零值都被视为真,通过与零的隐式不等式比较.)
(In C, a comparison operator always yields an int
value of 0
if the condition is false, 1
if it's true -- but any non-zero value is treated as true, via the implicit inequality comparison to zero.)
[*] 空指针常量被定义为一个值为 0
的整数常量表达式,或者这样的表达式转换为 void*
>.空指针常量不一定是指针类型,但转换它为指针类型会产生一个空指针值.将指针值与 0
进行比较会导致 0
隐式转换为指针类型,以便进行比较.
[*] A null pointer constant is defined as an integer constant expression with the value 0
, or such an expression cast to void*
. A null pointer constant isn't necessarily of pointer type, but converting it to pointer type yields a null pointer value. Comparing a pointer value to 0
causes the 0
to be implicitly converted to the pointer type so the comparison can be done.
这篇关于while(foo) vs while(foo != NULL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!