簡體   English   中英

這段代碼中 `sut` 變量的用途是什么? 為什么沒有它的第二個片段不起作用?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM