有人请解释一下为什么
int i=0,j=10;
while(j>0,i++){
printf("%d%d",i,j);
j--;
}
不会工作并且
int i=0,j=10;
while(i++,j>0){
printf("%d%d",i,j);
j--;
}
有效。
也请告诉我为什么
int i=0,j=10;
while(j>0,++i){
printf("%d%d",i,j);
j--;
}
给出一个无限循环?
感谢和问候
戒色
最佳答案
在您的 while 循环条件中,您使用逗号运算符,它计算其参数并返回第二个参数。所以在你的例子中:
while(j>0,i++) - returns i before it gets incremented;
that is 0, so loop won't execute at all
while(i++,j>0) - returns (j > 0) - runs as expected
while(j>0,++i) - returns i - will run until i overflows max int value
关于c while循环入口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3390239/