gpt4 book ai didi

c - c 中 while() 循环的不明确行为

转载 作者:行者123 更新时间:2023-11-30 20:16:52 25 4
gpt4 key购买 nike

由于 while 循环因错误表达式而终止其执行,尽管以下给出了不同的输出。无法追踪

#include <stdio.h>
int main()
{
//code snippet 1
while(0){
;
}
printf("C");
return 0;
}

这给出了输出

c

#include <stdio.h>
int main()
{
//code snippet 2
while(printf("%d")){
;
}
printf("c");
return 0;
}

这给出了输出

0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

由于 printf() 函数返回打印的字符数,并且没有打印任何字符,因此它不应导致无限循环,因为 0 的逻辑值为 false。

最佳答案

printf("%d") 的返回值不为 0(实际上是未定义的行为)。
请参阅Printf with no arguments explanation

关于c - c 中 while() 循环的不明确行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59710887/

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