我在理解下面给出的 while
循环的条件时遇到了一些困难:
int main()
{
char s[]="Let's Get it Started";
int i=0;
while(s[i]!=0)
{
//do something
++i
}
}
我知道字符串的最后一个字符存储为 \0
,其 ASCII 值为 0
。在 while
循环中,它比较数组中特定字符的值。所以当它达到 \0
条件时就像
'\0' != 0 // I guess this is also true
这不是死循环吗?
最佳答案
在 C
中,'\0'
与 0
具有相同的值(甚至类型)。两者都是具有 0
值的 int
。
So isn't this an infinite loop ?
所以,不,它不是无限循环,因为假设 \0
和 0
不同。但是对于不在这个问题范围内的其他因素,循环可能是无限的。
来自 C11 规范部分 5.2.1/2 字符集
A byte with all bits set to 0, called the null character, shall exist in the basic execution character set; it is used to terminate a character string.
关于c - '\0' != 0 不是真的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30772584/