![](/img/trans.png)
[英]Why doesn't my C code snippet work? Simplistic version does. Passing args without VA_ARGS for unsigned long long
[英]What's the purpose of the `sut` variable in this code? Why doesn't the second snippet without it work?
這個 function 打印一個X
字符的矩形:
void kutu_ciz( int line, int column ) {
int sut;
for ( ; line > 0; line--) {
for (sut = column; sut > 0; sut--)
printf("X");
printf("\n");
}
}
這個沒有sut
的版本是不行的。 為什么不? 有什么不同?
void kutu_ciz( int line, int column ) {
for ( ; line > 0; line--) {
for (; column > 0; column--)
printf("X");
printf("\n");
}
}
第二個版本“看起來”不錯,因為它在兩個for
循環中具有令人愉悅的對稱性。 但是,它不起作用,因為在內部循環運行后,一旦column
變量最終設置為0
。 下一次內部循環運行時column
已丟失其初始值並永久0
。 這會導致僅打印矩形的第一行,並且 rest 為空白。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.