簡體   English   中英

c結構如何循環? 為什么變量沒有在程序的position(2)處賦值? 它只在 position (1) 中打印?

[英]How does c structure loop? why is the variable not assigned the value at position (2) of the program? it only prints in position (1)?


#include <stdio.h>

int main()
{
    char c;
    char d[10000];
    int i = 0, nl = 0,ll = 0,k = 0;
    int maxi = 0;
    while((c=getchar()) != EOF){
        d[i]=c;
        if(c == '\n'){
            ++nl;
            ll = i - k; //line length
            k = i;
            if(maxi<=ll){
                maxi=ll;
                printf("%d",maxi); //**(1)**
            }
        }
        ++i;
    }
    // printf("%d",maxi); **(2)**
}

為什么變量沒有在程序的position(2)處賦值? 它只在 position (1) 中打印?

您應該養成在 printf 格式末尾添加換行符 ( \n ) 的習慣。 這通常會使數據更易於閱讀,並且主要可以使您不必考慮stdout緩沖。 (qv)

另外,我聽說有些 IDE 會吞下 output 的最后一行,如果它沒有以換行符終止的話。

當然,有時您會想要使用多個printf調用來打印一行; 在這種情況下,您只需將最后一個 printf 的\n放在該行中。 但正常情況是每個格式字符串都以\n結尾。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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