gpt4 book ai didi

c - C for 循环是否在遇到 '\0' 字符时退出?

转载 作者:太空宇宙 更新时间:2023-11-04 06:25:33 26 4
gpt4 key购买 nike

我在执行哈希函数时遇到了这样一种情况:当出现“\0”(NIL)字符时,for 循环
应该退出。

unsigned int hash_string (const char *s)
{
register unsigned int i;

for (i = 0; *s; s++) { // This for loop is supposed to end
// when a '\0' comes?
i *= 16777619;
i ^= *s;
}

return i;
}

据我所知,如果条件返回 0,C 循环应该结束。
然而在这里,没有这个条件,它仍然有效?

有人还可以告诉我们循环成功/失败的所有条件吗?

最佳答案

空字符的值为 0,因此在您的示例中,如果 *s 对应于字符串的空终止,则它的计算结果为零。

来自5.2.1字符集

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

然后在6.4.4.4字符常量

12 EXAMPLE 1 The construction '\0' is commonly used to represent the null character.

关于c - C for 循环是否在遇到 '\0' 字符时退出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27438888/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com