Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如果我从语句中省略运算符,如下所示: while(foo)编译器会将其转入while(foo != NULL || *foo != '\0' || foo != 0)或类似的while(!foo)东西吗?
while(foo)
while(foo != NULL || *foo != '\0' || foo != 0)
while(!foo)
它是如何真正做到的?
while (condition)只检查condition非零。
while (condition)
condition
因此这两个是等价的:
while (foo) while (foo != 0)
NULL、'\0' 和 0 在功能上是相同的,只是它们用于不同的类型。
因此foo,例如,如果是一个 int,while (foo)则意味着while (foo!=0)
foo
while (foo)
while (foo!=0)
如果 foo 是一个指针,while (foo)意味着while (foo!=NULL)
while (foo!=NULL)
等等。不过,编译器从不检查 foo 指向的值。